ruby: converting from float to integer in ruby produces strange results

后端 未结 3 2092
闹比i
闹比i 2020-12-10 22:42
ree-1.8.7-2010.02 :003 > (10015.8*100.0).to_i
 => 1001579 
ree-1.8.7-2010.02 :004 > 10015.8*100.0
 => 1001580.0 
ree-1.8.7-2010.02 :005 > 1001580.0.to         


        
3条回答
  •  独厮守ぢ
    2020-12-10 23:29

    Due to truncation error in float calculation, 10015.8*100.0 is actually calculated as 1001579.999999... So if you simply apply to_i, it cuts off the decimal part and returns 1001579

提交回复
热议问题