Using the following code snippet:
(fromIntegral 100)/10.00
Using the Haskell '98 standard prelude, how do I represent the result with two decimals?
Thanks.
Using the following code snippet:
(fromIntegral 100)/10.00
Using the Haskell '98 standard prelude, how do I represent the result with two decimals?
Thanks.
Just for the record:
You can use
printf :: PrintfType r => String -> r
fromText.Printf
:%f
formats the second argument as a floating point number.%.2f
indicates that only two digits behind the decimal point should be printed.\n
represents a newline. It is not strictly necessary for this example.Note that this function returns a value of type
String
orIO a
, depending on context. Demonstration:In this case
printf
returns the string"1337.00"
, because the result is passed as an argument to(++)
, which is a function that expects list arguments (note thatString
is the same as[Char]
). As such,printf
also behaves assprintf
would in other languages. Of course a trick such as appending a second string is not necessary. You can just explicitly specify the type: