我是 Ruby 的新手,所以我首先为我的无知道歉 :) 我在汇总客户帐户时发现了一个问题。一些金额在小数点后为负数,这会破坏总数。这是一些简单的示例代码...
testnum = 0.00
puts "###Debug### testnum = #{testnum} (after 0.00)"
testnum += 5.00
puts "###Debug### testnum = #{testnum} (after 5.00)"
testnum += 3.33
puts "###Debug### testnum = #{testnum} (after 3.33)"
testnum += -1.00
puts "###Debug### testnum = #{testnum} (after -1.00)"
testnum += -2.22
puts "###Debug### testnum = #{testnum} (after -2.22)"
结果...
###Debug### testnum = 0.0 (after 0.00)
###Debug### testnum = 5.0 (after 5.00)
###Debug### testnum = 8.33 (after 3.33)
###Debug### testnum = 7.33 (after -1.00)
###Debug### testnum = 5.109999999999999 (after -2.22)
所以添加-2.22后testnum被破坏了,但是添加-1.00就可以了。不知道我做错了什么。