Why output of following code is in scientific notation?
BigDecimal val = new BigDecimal("0000.000000111");
System.out.println(val);
output: 1.11e-8
but output of this code is proper in decimal format.
BigDecimal val = new BigDecimal("0000.000011111");
System.out.println(val);
output: 0.000011111
Is there a way to get proper decimal value for first string(like second output)?
If you look at the toString()
function of BigDecimal, it explains how the output is created:
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.
If you want the output without the e10
notation, use toPlainString().