小编NoT*_*iTo的帖子

ruby to_f bug或乘法运算符bug?

嗨,我刚遇到一个问题,其中ruby的to_f功能给了我不一致的结果.

ruby-1.9.2-head :026 > 8.45.to_f * 100
 => 844.9999999999999 

ruby-1.9.2-head :027 > 4.45.to_f * 100
 => 445.0 


ruby-1.9.2-head :028 > 4.35.to_f * 100
 => 434.99999999999994 
Run Code Online (Sandbox Code Playgroud)

我的解决方法是简单地以这种方式舍入结果

ruby-1.9.2-head :029 > (4.35.to_f * 100).round
 => 435 
Run Code Online (Sandbox Code Playgroud)

经过更多的游戏后,我意识到问题可能出在乘法运算符上 * 100

ruby rounding multiplication

2
推荐指数
1
解决办法
879
查看次数

标签 统计

multiplication ×1

rounding ×1

ruby ×1