Round number to specified number of digits

时光毁灭记忆、已成空白 提交于 2019-12-03 04:55:30

Not sure whether any standard function exists, but you can do it this way:

 (fromInteger $ round $ f * (10^n)) / (10.0^^n)

It depends on what you are going to do with the rounded number.

If you want to use it in calculations, you should use Data.Decimal from Decimal library.

If you want just to format the number nicely, you should use Text.Printf from the standard library (base package).

λ: ((/100) $ fromIntegral $ round (0.006 * 100)) == 0.006
λ: False

λ: ((/100) $ fromIntegral $ round (0.06 * 100)) == 0.06
λ: True
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!