Ruby BigDecimal Round:这是一个错误吗?
在编写一个用 BigDecimal 表示的值的测试时,我遇到了一些奇怪的事情并决定深入研究它。简而言之,“0.00009”四舍五入到小数点后两位时返回为 0.01 而不是 0.00。真的。这是我的脚本/控制台捕获:
>> bp = BigDecimal('0.09')
=> #<BigDecimal:210fe08,'0.9E-1',4(8)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.09
>> bp = BigDecimal('0.009')
=> #<BigDecimal:210bcf4,'0.9E-2',4(8)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.01
>> bp = BigDecimal('0.0009')
=> #<BigDecimal:2107a8c,'0.9E-3',4(12)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.0
>> bp = BigDecimal('0.00009')
=> #<BigDecimal:2103428,'0.9E-4',4(12)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.01
>> bp = BigDecimal('0.000009')
=> #<BigDecimal:20ff0f8,'0.9E-5',4(12)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.0
哦,如果我使用默认模式,我会得到相同的结果,如下所示:
>> bd = BigDecimal('0.00009')
=> #<BigDecimal:2152ed8,'0.9E-4',4(12)>
>> bd.round(2).to_f
=> 0.01
这是我的版本:
ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-darwin9.2.2]
Rails 2.3.4
有人见过这样的东西吗?
While writing a test with a value that gets represented as a BigDecimal, I ran into something weird and decided to dig into it. In brief, '0.00009' when rounded to two decimal places is returned as 0.01 instead of 0.00. Really. Here's my script/console capture:
>> bp = BigDecimal('0.09')
=> #<BigDecimal:210fe08,'0.9E-1',4(8)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.09
>> bp = BigDecimal('0.009')
=> #<BigDecimal:210bcf4,'0.9E-2',4(8)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.01
>> bp = BigDecimal('0.0009')
=> #<BigDecimal:2107a8c,'0.9E-3',4(12)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.0
>> bp = BigDecimal('0.00009')
=> #<BigDecimal:2103428,'0.9E-4',4(12)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.01
>> bp = BigDecimal('0.000009')
=> #<BigDecimal:20ff0f8,'0.9E-5',4(12)>
>> bp.round(2,BigDecimal::ROUND_HALF_DOWN).to_f
=> 0.0
Oh, and I get the same results if I use the default mode, like so:
>> bd = BigDecimal('0.00009')
=> #<BigDecimal:2152ed8,'0.9E-4',4(12)>
>> bd.round(2).to_f
=> 0.01
Here are my versions:
ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-darwin9.2.2]
Rails 2.3.4
Has anyone seen anything like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,以前从未见过这个,而且它看起来绝对像一个错误。
0.00009
四舍五入到小数点后两位肯定应该是0.00
。ROUND_HALF_DOWN
不应改变行为,因为您不处理中点值。此链接有更多详细信息。
这似乎是 1.8 级别中的一个错误,已在 1.9 中修复。这是一个有点奇怪的问题,因为它似乎只影响第一个非零数字之前有偶数个零的数字,并且仅当该数字为 5 或更大时。
根据提供的数据,这似乎正是您的问题。
No, never seen this before, and it definitely looks like a bug.
0.00009
rounded to two decimal places should definitely be0.00
.The
ROUND_HALF_DOWN
should not change the behaviour since you're not dealing with midpoint values.This link has more details.
It seems to be a bug in the 1.8 levels that's been fixed in 1.9. It's a slightly bizarre one in that it only seems to affect numbers with an even number of zeros before the first non-zero digit and only if that digit is 5 or greater.
That appears to be exactly your problem based on the data provided.
我认为,这也是一个错误,但我想知道的是显示结果的 .to_f 。对于 BigDecimal,您应该使用 .to_s('F') 来代替,因为我猜您有理由使用 BigDecimal 而不是 Floats。
I think, this is a bug too, but what I wonder about is the .to_f to display the result. With BigDecimal you should use .to_s('F') instead since I guess you have a reason for using BigDecimal instead of Floats.