编程语言
首页 > 编程语言> > java – 为什么输出是科学记数法?

java – 为什么输出是科学记数法?

作者:互联网

为什么以下代码的输出是科学记数法?

      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

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

解决方法:

如果你看一下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().

标签:scientific-notation,java,string,decimal,bigdecimal
来源: https://codeday.me/bug/20190727/1558621.html