Why inference in scientific notation?

Why is the output of the following code in scientific notation?

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

      

output: 1.11e-8

but the output of this code is correct in decimal format.

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

      

output: 0.000011111

Is there a way to get the correct decimal value for the first line (like the second output)?

+3


source to share


2 answers


If you look at the toString()

BigDecimal function
, it explains how the result is generated:

The adjusted figure is then calculated; this is a negative scale, plus the number of characters in the converted unscaled value, less than one. That is, -scale + (ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal places (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 symbolic 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, the decimal point will be inserted indicating the scale of the number of characters to the right of the decimal point.



If you want unmarked output e10

, use toPlainString () .

+4


source


use toPlainString

method



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

      

+3


source







All Articles