为什么输出是科学计数法?

why output is in scientific notation?

为什么下面代码输出的是科学计数法?

      BigDecimal val = new BigDecimal("0000.000000111");
      System.out.println(val);

输出:1.11e-8

但是这段代码的输出是正确的十进制格式。

      BigDecimal val = new BigDecimal("0000.000011111");
      System.out.println(val);

输出:0.000011111

有没有办法为第一个字符串(如第二个输出)获取正确的十进制值?

使用toPlainString方法

BigDecimal val = new BigDecimal("0000.000011111");
          System.out.println(val.toPlainString());

如果您查看 the toString() function of BigDecimal,它会解释如何创建输出:

Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, -scale+(ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).

If the scale is greater than or equal to zero and the adjusted exponent is greater than or equal to -6, the number will be converted to a character form without using exponential notation. In this case, if the scale is zero then no decimal point is added and if the scale is positive a decimal point will be inserted with the scale specifying the number of characters to the right of the decimal point.

如果您想要没有 e10 符号的输出,请使用 toPlainString().