-
-
Notifications
You must be signed in to change notification settings - Fork 878
ICU-22872 Fix BigDecimal scale in DecimalFormat.parse() #3853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8677,4 +8677,41 @@ public void testStrictParse() throws java.text.ParseException { | |
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test that BigDecimal scale is correctly preserved when parsing numbers with trailing zeros. | ||
| * This is a regression test for ICU-22872. | ||
| */ | ||
| @Test | ||
| public void TestParseBigDecimalScale() throws ParseException { | ||
| DecimalFormat df = new DecimalFormat("0.00"); | ||
| df.setParseBigDecimal(true); | ||
|
|
||
| // Test cases: input string, expected scale | ||
| Object[][] testCases = { | ||
| {"0.0", 1}, | ||
| {"1.0", 1}, | ||
| {"1.00", 2}, | ||
| {"1.10", 2}, | ||
| {"0.10", 2}, | ||
| {"123.450", 3}, | ||
| {"5.0000", 4}, | ||
| }; | ||
|
Comment on lines
+8690
to
+8699
|
||
|
|
||
| for (Object[] testCase : testCases) { | ||
| String input = (String) testCase[0]; | ||
| int expectedScale = (Integer) testCase[1]; | ||
| Number result = df.parse(input); | ||
| assertTrue( | ||
| "Result should be BigDecimal for input: " + input, | ||
| result instanceof BigDecimal); | ||
| BigDecimal bd = (BigDecimal) result; | ||
| assertEquals("Scale mismatch for input: " + input, expectedScale, bd.scale()); | ||
| } | ||
|
|
||
| // Verify values are correct, not just scales | ||
| assertEquals("Value check for 0.0", new BigDecimal("0.0"), df.parse("0.0")); | ||
| assertEquals("Value check for 1.00", new BigDecimal("1.00"), df.parse("1.00")); | ||
| assertEquals("Value check for 123.450", new BigDecimal("123.450"), df.parse("123.450")); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new test depends on the JVM default locale using '.' as the decimal separator (inputs like "0.0"/"1.00"). If the test suite runs under a locale with ',' decimal separators, parsing these literals can fail or behave differently. Consider constructing the DecimalFormat with explicit DecimalFormatSymbols for a fixed locale (for example en-US) to make the test deterministic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better would be
(DecimalFormat) NumberFormat.getInstance(ULocale.ROOT)