Haskell printf to string
Text.Printf might be what you need.
Yes, it's in the Text.Printf module, and it's just called printf
.
> import Text.Printf
> let x = 1.14907259
> putStrLn . printf "%.2f" $ x
1.15
Note that the return type of printf
is overloaded, so that it's capable of returning a String
(as in the example above) but it's also capable of returning an I/O action that does the printing, so you don't actually need the call to putStrLn
:
> printf "%.2f\n" x
1.15