简体   繁体   English

Ruby BigDecimal Round:这是一个错误吗?

[英]Ruby BigDecimal Round: Is this an error?

While writing a test with a value that gets represented as a BigDecimal, I ran into something weird and decided to dig into it. 在编写一个带有表示为BigDecimal的值的测试时,我遇到了一些奇怪的东西并决定深入研究它。 In brief, '0.00009' when rounded to two decimal places is returned as 0.01 instead of 0.00. 简而言之,当舍入到两位小数时,'0.00009'将返回0.01而不是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? 有没有人见过这样的东西?

No, never seen this before, and it definitely looks like a bug. 不,之前从未见过这个,它看起来确实像个错误。 0.00009 rounded to two decimal places should definitely be 0.00 . 舍入到小数点后两位的0.00009肯定应为0.00

The ROUND_HALF_DOWN should not change the behaviour since you're not dealing with midpoint values. 由于您没有处理中点值,因此ROUND_HALF_DOWN不应更改行为。

This link has more details. 此链接有更多详细信息。

It seems to be a bug in the 1.8 levels that's been fixed in 1.9. 它似乎是1.8级中的一个错误,已在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. 这有点奇怪之一,因为它似乎只影响在第一个非零数字之前具有偶数个零的数字,并且仅在该数字为5或更大时。

That appears to be exactly your problem based on the data provided. 这似乎根据所提供的数据是准确的问题。

I think, this is a bug too, but what I wonder about is the .to_f to display the result. 我想,这也是一个错误,但我想知道的是.to_f来显示结果。 With BigDecimal you should use .to_s('F') instead since I guess you have a reason for using BigDecimal instead of Floats. 使用BigDecimal,你应该使用.to_s('F'),因为我猜你有理由使用BigDecimal而不是Floats。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM