java 大十进制减法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32546553/
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-11-02 20:22:23  来源:igfitidea点击:

BigDecimal Subtraction

javabigdecimal

提问by Rakesh KR

I want to substract 2 doublevalues, and I have tried the following code.

我想减去 2 个double值,我尝试了以下代码。

double val1 = 2.0;
double val2 = 1.10;

System.out.println(val1 - val2);

and I got the output as,

我得到的输出是,

0.8999999999999999

For getting output as 0.9I tried with BigDecimalas follows,

为了获得0.9我尝试BigDecimal如下的输出,

BigDecimal val1BD = new BigDecimal(val1);
BigDecimal val2BD = new BigDecimal(val2);

System.out.println(val1BD.subtract(val2BD));

And I got the output as,

我得到的输出是,

0.899999999999999911182158029987476766109466552734375

Then I tried with BigDecimal.valueOf()

然后我试过 BigDecimal.valueOf()

val1BD = BigDecimal.valueOf(val1);
val2BD = BigDecimal.valueOf(val2);

System.out.println(val1BD.subtract(val2BD));

And finally I got the output as 0.9.

最后我得到了输出为0.9.

My question is what is the difference between case 2 & case 3?

我的问题是案例 2 和案例 3 之间有什么区别?

In case 2 why I got the output like that?

在情况 2 为什么我得到这样的输出?

采纳答案by CoderCroc

BigDecimal.valueOf(double d)uses canonical Stringrepresentation of double value, internally Double.toString(double)is used, that's why you are getting 0.9in second case.

BigDecimal.valueOf(double d)使用String双值的规范表示,在内部Double.toString(double)使用,这就是为什么你会遇到0.9第二种情况。

Note:This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).

注意:这通常是将双精度(或浮点数)转换为 BigDecimal 的首选方法,因为返回的值等于根据 using 的结果构造 BigDecimal 的结果 Double.toString(double)

While with new BigDecimal(0.9)it converts value to exact floating pointrepresentation of doublevalue without using Stringrepresentation,

同时用new BigDecimal(0.9)其转换值到精确浮点的表示double值,而无需使用String表示,

Translates a double into a BigDecimalwhich is the exact decimal representation of the double's binary floating-point value.

...

NOTES :

  1. The results of this constructor can be somewhat unpredictable.

...

将 double 转换为BigDecimaldouble 的二进制浮点值的精确十进制表示形式。

...

注意事项:

  1. 此构造函数的结果可能有些不可预测。

...

FOR EXAMPLE :

例如 :

BigDecimal bd1 = new BigDecimal(Double.toString(0.9));
BigDecimal bd2 = new BigDecimal(0.9);
System.out.println(bd1);
System.out.println(bd2);

OUTPUT :

输出 :

0.9
0.90000000000000002220446049250313080847263336181640625

回答by SLuck

Just for those others that got here looking for some other issue with BigDecimal(not related to the question above)... remember to give a mathContext to the methods to avoid certain problems e.g.

只是为了那些来到这里寻找 BigDecimal 其他问题的其他人(与上述问题无关)......记得给方法一个 mathContext 以避免某些问题,例如

        MathContext mc = new MathContext(10, RoundingMode.HALF_UP);
        BigDecimal hitRate = new BigDecimal(totalGetValuesHitted).divide(new BigDecimal(totalGetValuesRequested), mc);
        BigDecimal missRate = new BigDecimal(1.0, mc).subtract(hitRate, mc);