java BigDecimal 中 Divide 方法的 Scale()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3303503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:12:36  来源:igfitidea点击:

Scale() of Divide method in BigDecimal

javaformattingbigdecimal

提问by Saobi

new BigDecimal("37146555.53880000").divide(new BigDecimal("1000000")).scale()

This returns 10. But according to the API, the dividemethod:

这返回10. 但根据API,该divide方法:

Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale());

返回一个 BigDecimal,其值为 (this / divisor),其首选比例为 (this.scale() - divisor.scale());

So in this case, 37146555.53880000'sscale is 8, and 1000000's scale is 0. So the result should have a scale of 8, not 10.

所以在这种情况下,37146555.53880000'sscale 是8,并且1000000的 scale 是0。所以结果的尺度应该是8,而不是10

What am I missing here?

我在这里错过了什么?

Thanks

谢谢

采纳答案by Andrei Fierbinteanu

The actual result is 37.1465555388 whose scale must be 10 for it to be exact.

实际结果是 37.1465555388,其比例必须为 10 才能准确。

What the JavaDoc says is that the preferred scale is the difference meaning that if the result didn't actually need to be 10, then it would try to make it 8. For example if you would have divided by 2, whose scale is also 0, the result would have been 18573277.76940000 (scale 8).

JavaDoc 所说的是首选比例是差异意味着如果结果实际上不需要为 10,那么它会尝试将其设为 8。例如,如果您除以 2,其比例也是 0 ,结果将是 18573277.76940000(比例 8)。

EDIT: small adition - you can force the division to a certain scale by using the overloaded divide methods:

编辑:小加法 - 您可以使用重载的除法方法强制除法达到一定的规模:

  • divide(BigDecimal, RoundingMode)that will give a BigDecimalwith scale of thisand value rounded using the specified rounding method if the result would actually need more decimals to be exact.

  • divide(BigDecimal, scale, RoundingMode)that will give a BigDecimalwith specified scale, and value rounded by specified method if needed.

  • divide(BigDecimal, RoundingMode)如果结果实际上需要更多的小数才能准确,这将给出一个使用指定的舍入方法舍入的BigDecimal比例this和值。

  • divide(BigDecimal, scale, RoundingMode)BigDecimal如果需要,这将给出一个具有指定比例的值,以及通过指定方法四舍五入的值。

This might be useful if your dividing by a number you know can cause repeating decimals, like 3 (1/3 = 0.333333...) since, if that happens, the simple divide will throw an exception. Bounding it to a maximum number of decimals will help you avoid the exception but will make your computations less precise.

如果您除以已知的数字会导致重复小数,例如 3 (1/3 = 0.333333...),这可能很有用,因为如果发生这种情况,简单的除法将引发异常。将其限制为最大小数位数将帮助您避免异常,但会使您的计算不那么精确。

回答by Peter G.

These scales are the ones used by the methods which return exact arithmetic results; except that an exact divide may have to use a larger scalesince the exact result may have more digits. For example, 1/32 is 0.03125.

这些尺度是返回精确算术结果的方法所使用的尺度;除了精确的除法可能必须使用更大的比例,因为精确的结果可能有更多的数字。例如,1/32 是 0.03125。

回答by hvgotcodes

It says "preferred scale" not "definitely will be scale".

它说“首选规模”而不是“肯定会规模”。

To be absolutely sure, I would use BigDecimal.divide(BigDecimal, int, int).

可以肯定的是,我会使用BigDecimal.divide(BigDecimal, int, int).