Ruby 中浮点的一致舍入
据我了解,由于浮点的不精确表示,以下代码“感觉”不一致。
"%.1f" % 1.14 # => 1.1
"%.1f" % 1.15 # => 1.1
"%.1f" % 1.16 # => 1.2
"%.0f" % 1.4 # => 1
"%.0f" % 1.5 # => 2
"%.0f" % 1.6 # => 2
然而,有没有一种简单的方法可以使浮点数四舍五入一致?一种方法可能是显式地进行字符串操作。有更简单的方法或现有的库吗?
I understand due to the inexact representation of floating points, the following code 'feels' inconsistent.
"%.1f" % 1.14 # => 1.1
"%.1f" % 1.15 # => 1.1
"%.1f" % 1.16 # => 1.2
"%.0f" % 1.4 # => 1
"%.0f" % 1.5 # => 2
"%.0f" % 1.6 # => 2
However, is there an easy way of doing consistent floating points rounding by 5? One way might be to do string manipulation explicitly. Is there an easier way or existent library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要小数精度,请使用 BigDecimal 而不是浮点数。
编辑:在将数字传递给
%
之前,您必须手动将数字舍入到所需的长度,否则它会在舍入之前转换为普通浮点数。If you want decimal precision, use BigDecimal instead of floats.
Edit: You will have to manually round the number to the desired length before passing it to
%
, otherwise it gets converted to a normal float before being rounded.只需添加一个微小的扰动,即可确保浮点值略低于 0.5 的部分变得刚好结束。
例如,
这足以处理格式问题,同时不太可能导致相关错误。
另外:这是我之前的问题的明显后续这里,这很好,但包含在完整性。
Just add a tiny pertubation, to ensure things that are just under 0.5 in floating-point become just over.
For example,
this will be enough to deal with the formatting problems, while very unlikely to cause a relevant error.
also: an obvious follow-on to my earlier question here, which is fine, but included for completeness.
此示例中的函数
roundthis()
展示了如何以可控、一致的方式对数字进行舍入。注意小软糖值。尝试在不做任何改动的情况下运行这个示例,看看会发生什么。将其放入名为 roundtest.rb 的文件中并执行打印。
请注意舍入到最接近的 2、15、0.005 或其他值的容易程度。
The function
roundthis()
in this example shows how to round numbers in a controllable, consistent way. Note the small fudge value. Try running this example without the fudge to see what happens.This, put into a file named roundtest.rb and executed prints
Note the ease of rounding to the nearest 2, 15, 0.005, or whatever.
乘以 100,然后四舍五入,然后除以 100:
这不太优雅,但它避免了使用字符串。
Multiply by 100, then round, then divide by 100:
It's not exactly elegant, but it avoids using strings.