Why was the pattern string not followed in this code?
In order to fit your first pattern better i would just remove the first #. Like this you fit international unit system while keeping optional numbers after point.
DecimalFormat eNotation1 = new DecimalFormat("0.###E0");
System.out.println(eNotation1.format(123.456789));
If you want that exact format, then use the pattern 0.000E0
:
DecimalFormat eNotation1 = new DecimalFormat("0.000E0");
System.out.println(eNotation1.format(123.456789));
1.235E2
As to why you are seeing your current behavior, the #
placeholders are optional digits, which means that DecimalFormat
is not obligated to actually use them exactly as you used them in the pattern. The only requirement appears to be that the total number of digits appearing in the scientific notation output matches. In this case, the total number of digits is five, so we get the output 1.2346E2
.