截断一个浮点数,而不是四舍五入 [英] Truncate a floating point number without rounding up
本文介绍了截断一个浮点数,而不是四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,将 1.0155555555555555
转换为 1.015
1.016
)。
如何在Ruby中执行此操作?
<假设你有一个 float
,试试这个:
(x * 1000).floor / 1000.0
结果:
1.015
查看在线工作: ideone
I have a floating point number that I want to truncate to 3 places but I don't want to round up.
For example, convert 1.0155555555555555
to 1.015
(not 1.016
).
How would I go about doing this in Ruby?
解决方案
Assuming you have a float
, try this:
(x * 1000).floor / 1000.0
Result:
1.015
See it working online: ideone
这篇关于截断一个浮点数,而不是四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文