BigDecimal在用double做入参的时候,二进制无法精确地表示十进制小数,编译器读到字符串"0.0000002"和“1.0000002”之后,必须把它转成8个字节的double值,也就是1.0000001999999998947288304407265968620777130126953125类似这种。
所以,运行的时候,实际传给BigDecimal构造函数的真正的数值是1.0000001999999998947288304407265968620777130126953125。
BigDecimal在用String做入参的时候,能够正确地把字符串转化成真正精确的浮点数。
System.out.println部分,如果入参是string,那么直接输出,如果入参是其他类型,那么会调用Object.toString方法进行转化之后进行输出。而Double.toString会使用一定的精度来四舍五入double,然后再输出。
BigDecimal构造方法上的注释就写了这个问题:
The results of this constructor can be somewhat unpredictable.
* One might assume that writing {@code new BigDecimal(0.1)} in
* Java creates a {@code BigDecimal} which is exactly equal to
* 0.1 (an unscaled value of 1, with a scale of 1), but it is
* actually equal to
* 0.1000000000000000055511151231257827021181583404541015625.
* This is because 0.1 cannot be represented exactly as a
* {@code double} (or, for that matter, as a binary fraction of
* any finite length). Thus, the value that is being passed
* in to the constructor is not exactly equal to 0.1,
* appearances notwithstanding.
The {@code String} constructor, on the other hand, is
* perfectly predictable: writing {@code new BigDecimal("0.1")}
* creates a {@code BigDecimal} which is exactly equal to
* 0.1, as one would expect. Therefore, it is generally
* recommended that the {@linkplain #BigDecimal(String)
* String constructor} be used in preference to this one.
补充说明一下:
Double.toString这个方法输出的是一个String,而且会进行四舍五入处理。
new BigDecimal(Double.toString(d1))这个入参在处理完毕之后是一个String,调用的是BigDecimal(String val)这个构造方法。
源码里BigDecimal(String val)这个方法是会将val处理成char[]数组:
this(val.toCharArray(), 0, val.length());
然后调用BigDecimal(char[] in)这个构造方法。
而new BigDecimal(d1)调用的是 BigDecimal(double val),这个方法进来之后第一件事就是
long valBits = Double.doubleToLongBits(val);
把入参转换成二进制,所以会造成精度丢失。
double相加造成的精度丢失和上面的情况一样,先转成bit之后进行计算,然后精度丢失。。