Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Force floats to have two decimals

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.

like image 752
Anders Avatar asked Oct 13 '09 11:10

Anders


People also ask

How do you get a float number up to 2 decimal places?

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 %.

Can floats hold decimals?

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.


1 Answers

Just for the record:

import Numeric  formatFloatN floatNum numOfDecimals = showFFloat (Just numOfDecimals) floatNum "" 
like image 55
Marat Salikhov Avatar answered Sep 25 '22 00:09

Marat Salikhov