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.
format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.
If doing math with floats, you need to add a decimal point, otherwise it will be treated as an int. See the Floating point constants page for details. The float data type has only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.
Just for the record:
import Numeric formatFloatN floatNum numOfDecimals = showFFloat (Just numOfDecimals) floatNum ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With