将数字四舍五入到指定位数 [英] Round number to specified number of digits

查看:86
本文介绍了将数字四舍五入到指定位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个简单的函数可以将DoubleFloat舍入为指定的位数?我已经在这里和Hoogle上搜索了((Fractional a) => Int -> a -> a),但是没有找到任何内容.

Is there a simple function to round a Double or Float to a specified number of digits? I've searched here and on Hoogle (for (Fractional a) => Int -> a -> a), but haven't found anything.

推荐答案

不确定是否存在任何标准函数,但是您可以通过以下方式做到这一点:

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

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

这篇关于将数字四舍五入到指定位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆