Skip to content

Commit 46c4da7

Browse files
author
Justin Lu
committed
8159023: Engineering notation of DecimalFormat does not work as documented
Reviewed-by: naoto
1 parent ee321c7 commit 46c4da7

File tree

2 files changed

+147
-7
lines changed

2 files changed

+147
-7
lines changed

src/java.base/share/classes/java/text/DecimalFormat.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@
264264
* formatted using the localized minus sign, <em>not</em> the prefix and suffix
265265
* from the pattern. This allows patterns such as {@code "0.###E0 m/s"}.
266266
*
267+
* <li>The <em>maximum integer</em> digits is the sum of '0's and '#'s
268+
* prior to the decimal point. The <em>minimum integer</em> digits is the
269+
* sum of the '0's prior to the decimal point. The <em>maximum fraction</em>
270+
* and <em>minimum fraction</em> digits follow the same rules, but apply to the
271+
* digits after the decimal point but before the exponent. For example, the
272+
* following pattern: {@code "#00.0####E0"} would have a minimum number of
273+
* integer digits = 2("00") and a maximum number of integer digits = 3("#00"). It
274+
* would have a minimum number of fraction digits = 1("0") and a maximum number of fraction
275+
* digits= 5("0####").
276+
*
267277
* <li>The minimum and maximum number of integer digits are interpreted
268278
* together:
269279
*
@@ -282,13 +292,38 @@
282292
* {@code "12.3E-4"}.
283293
* </ul>
284294
*
285-
* <li>The number of significant digits in the mantissa is the sum of the
286-
* <em>minimum integer</em> and <em>maximum fraction</em> digits, and is
287-
* unaffected by the maximum integer digits. For example, 12345 formatted with
288-
* {@code "##0.##E0"} is {@code "12.3E3"}. To show all digits, set
289-
* the significant digits count to zero. The number of significant digits
295+
* <li>For a given number, the amount of significant digits in
296+
* the mantissa can be calculated as such
297+
*
298+
* <blockquote><pre>
299+
* <i>Mantissa Digits:</i>
300+
* min(max(Minimum Pattern Digits, Original Number Digits), Maximum Pattern Digits)
301+
* <i>Minimum pattern Digits:</i>
302+
* <i>Minimum Integer Digits</i> + <i>Minimum Fraction Digits</i>
303+
* <i>Maximum pattern Digits:</i>
304+
* <i>Maximum Integer Digits</i> + <i>Maximum Fraction Digits</i>
305+
* <i>Original Number Digits:</i>
306+
* The amount of significant digits in the number to be formatted
307+
* </pre></blockquote>
308+
*
309+
* This means that generally, a mantissa will have up to the combined maximum integer
310+
* and fraction digits, if the original number itself has enough significant digits. However,
311+
* if there are more minimum pattern digits than significant digits in the original number,
312+
* the mantissa will have significant digits that equals the combined
313+
* minimum integer and fraction digits. The number of significant digits
290314
* does not affect parsing.
291315
*
316+
* <p>It should be noted, that the integer portion of the mantissa will give
317+
* any excess digits to the fraction portion, whether it be for precision or
318+
* for satisfying the total amount of combined minimum digits.
319+
*
320+
* <p>This behavior can be observed in the following example,
321+
* {@snippet lang=java :
322+
* DecimalFormat df = new DecimalFormat("#000.000##E0");
323+
* df.format(12); // returns "12.0000E0"
324+
* df.format(123456789) // returns "1.23456789E8"
325+
* }
326+
*
292327
* <li>Exponential patterns may not contain grouping separators.
293328
* </ul>
294329
*
@@ -308,8 +343,8 @@
308343
*
309344
* <h4>Special Values</h4>
310345
*
311-
* <p>{@code NaN} is formatted as a string, which typically has a single character
312-
* {@code U+FFFD}. This string is determined by the
346+
* <p>Not a Number({@code NaN}) is formatted as a string, which typically has a
347+
* single character {@code U+FFFD}. This string is determined by the
313348
* {@code DecimalFormatSymbols} object. This is the only value for which
314349
* the prefixes and suffixes are not used.
315350
*
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
/*
27+
* @test
28+
* @bug 8159023
29+
* @summary Confirm behavior of mantissa for scientific notation in Decimal Format
30+
* @run junit MantissaDigits
31+
*/
32+
33+
import java.text.DecimalFormat;
34+
import java.text.DecimalFormatSymbols;
35+
import java.util.Locale;
36+
import java.util.stream.Stream;
37+
38+
import org.junit.jupiter.api.Test;
39+
import org.junit.jupiter.params.ParameterizedTest;
40+
import org.junit.jupiter.params.provider.Arguments;
41+
import org.junit.jupiter.params.provider.MethodSource;
42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
44+
public class MantissaDigits {
45+
private static final double[] NUMBERS = {
46+
1.1, 12.1, 123.1, 1234.1, 12345.1, 123456.1,
47+
-1.1, -12.1, -123.1, -1234.1, -12345.1, -123456.1,
48+
1, 12, 123, 1234, 12345, 123456, 1234567,
49+
-1, -12, -123, -1234, -12345, -123456, -1234567,
50+
1.1234, 1.1111, 1.412, 222.333, -771.2222
51+
};
52+
private static final DecimalFormatSymbols DFS = new DecimalFormatSymbols(Locale.US);
53+
private static final String ERRMSG = "%s formatted with %s gives %s, and " +
54+
"significant digit count was %s, but the formula provided %s%n";
55+
// Hard coded as 1, since all test patterns only have 1 exponent digit
56+
private static final int EXPONENTDIGITS = 1;
57+
58+
@ParameterizedTest
59+
@MethodSource("patterns")
60+
public void testMantissaDefinition(String pattern, int minDigits, int maxDigits) {
61+
DecimalFormat df = new DecimalFormat(pattern, DFS);
62+
for (double number : NUMBERS) {
63+
// Count the significant digits in the pre-formatted number
64+
int originalNumDigits = (int) String.valueOf(number).chars()
65+
.filter(Character::isDigit).count();
66+
67+
if (wholeNumber(number)) {
68+
// Trailing 0 should not be counted
69+
originalNumDigits--;
70+
}
71+
72+
// Format the number, then grab the significant
73+
// digits inside the mantissa
74+
String formattedNum = df.format(number);
75+
int mantissaDigits = (int) formattedNum.chars()
76+
.filter(Character::isDigit).count() - EXPONENTDIGITS;
77+
78+
// Test the new definition of the Mantissa
79+
Integer calculatedDigits = Math
80+
.min(Math.max(minDigits, originalNumDigits), maxDigits);
81+
assertEquals(mantissaDigits, calculatedDigits, String.format(ERRMSG,
82+
number, pattern, formattedNum, mantissaDigits, calculatedDigits));
83+
}
84+
}
85+
86+
private static Boolean wholeNumber(double number) {
87+
return (int) number == number;
88+
}
89+
90+
private static Stream<Arguments> patterns() {
91+
return Stream.of(
92+
Arguments.of("#0.0##E0", 2, 5),
93+
Arguments.of("#00.00##E0", 4, 7),
94+
Arguments.of("#0.000##E0", 4, 7),
95+
Arguments.of("#00.000##E0", 5, 8),
96+
Arguments.of("#000.0##E0", 4, 7),
97+
Arguments.of("#000.00##E0", 5, 8),
98+
Arguments.of("#000.000##E0", 6, 9),
99+
Arguments.of("000.000E0", 6, 6),
100+
Arguments.of("#.##E0", 0, 3),
101+
Arguments.of("######.######E0", 0, 12),
102+
Arguments.of("####00.00######E0", 4, 14)
103+
);
104+
}
105+
}

0 commit comments

Comments
 (0)