如何舍入浮点值 [英] How to round a float value
本文介绍了如何舍入浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试按如下方式舍入浮点值:
I'm trying to round a float value as follows:
(0.11 + 0.22 + 0.23 / 3).round(2)
有谁知道有没有其他有效的方法来围捕?
Does anyone know if there is other effective way to round up?
推荐答案
如果你总是想四舍五入,永不向下,你可以这样做:
If you always want to round up and never down, you can do this:
(0.411 * 100).ceil / 100.0 # => 0.42
否则就使用round
.或者,如果您不介意将浮点数转换为字符串,则使用字符串格式化程序.
Otherwise just use round
. Or use the string formatter if you don't mind your float turning into a string.
这篇关于如何舍入浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文