ICU-20425 Strip trailing zeros from Java rounding increment

See #1726
This commit is contained in:
Shane F. Carr 2021-09-21 07:33:11 +00:00
parent 35dff23fb5
commit fd5c76edbd
5 changed files with 51 additions and 2 deletions

View file

@ -247,6 +247,8 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n
TESTCASE_AUTO(Test20961_CurrencyPluralPattern);
TESTCASE_AUTO(Test21134_ToNumberFormatter);
TESTCASE_AUTO(Test13733_StrictAndLenient);
TESTCASE_AUTO(Test20425_IntegerIncrement);
TESTCASE_AUTO(Test20425_FractionWithIntegerIncrement);
TESTCASE_AUTO(Test21232_ParseTimeout);
TESTCASE_AUTO(Test10997_FormatCurrency);
TESTCASE_AUTO_END;
@ -10039,6 +10041,26 @@ void NumberFormatTest::Test13733_StrictAndLenient() {
}
}
void NumberFormatTest::Test20425_IntegerIncrement() {
IcuTestErrorCode status(*this, "Test20425_IntegerIncrement");
DecimalFormat df("##00", status);
df.setRoundingIncrement(1);
UnicodeString actual;
df.format(1235.5, actual, status);
assertEquals("Should round to integer", u"1236", actual);
}
void NumberFormatTest::Test20425_FractionWithIntegerIncrement() {
IcuTestErrorCode status(*this, "Test20425_FractionWithIntegerIncrement");
DecimalFormat df("0.0", status);
df.setRoundingIncrement(1);
UnicodeString actual;
df.format(8.6, actual, status);
assertEquals("Should have a fraction digit", u"9.0", actual);
}
void NumberFormatTest::Test21232_ParseTimeout() {
IcuTestErrorCode status(*this, "Test21232_ParseTimeout");

View file

@ -303,6 +303,8 @@ class NumberFormatTest: public CalendarTimeZoneTest {
void Test20961_CurrencyPluralPattern();
void Test21134_ToNumberFormatter();
void Test13733_StrictAndLenient();
void Test20425_IntegerIncrement();
void Test20425_FractionWithIntegerIncrement();
void Test21232_ParseTimeout();
void Test10997_FormatCurrency();

View file

@ -175,6 +175,9 @@ final class NumberPropertyMapper {
if (PatternStringUtils.ignoreRoundingIncrement(roundingIncrement, maxFrac)) {
rounding = Precision.constructFraction(minFrac, maxFrac);
} else {
if (minFrac > roundingIncrement.scale()) {
roundingIncrement = roundingIncrement.setScale(minFrac);
}
rounding = Precision.constructIncrement(roundingIncrement);
}
} else if (explicitMinMaxSig) {

View file

@ -1248,7 +1248,10 @@ public class DecimalFormat extends NumberFormat {
if (increment == 0) {
setRoundingIncrement((java.math.BigDecimal) null);
} else {
java.math.BigDecimal javaBigDecimal = java.math.BigDecimal.valueOf(increment);
// ICU-20425: Since doubles have no concept of trailing zeros, we should strip
// trailing zeros from the BigDecimal.
java.math.BigDecimal javaBigDecimal = java.math.BigDecimal.valueOf(increment)
.stripTrailingZeros();
setRoundingIncrement(javaBigDecimal);
}
}

View file

@ -4104,7 +4104,7 @@ public class NumberFormatTest extends TestFmwk {
new SetMxFrAndRndIncrItem( "15 en_US DEC 1/1/3/0.02", "en_US", NumberFormat.NUMBERSTYLE, 1, 1, 3, 0.02, "#,##0.02#", 0.128, "0.12" ), // use incr
new SetMxFrAndRndIncrItem( "16 en_US DEC 1/2/2/0.02", "en_US", NumberFormat.NUMBERSTYLE, 1, 2, 2, 0.02, "#,##0.02", 0.128, "0.12" ), // use incr
new SetMxFrAndRndIncrItem( "17 en_US DEC 1/2/3/0.02", "en_US", NumberFormat.NUMBERSTYLE, 1, 2, 3, 0.02, "#,##0.02#", 0.128, "0.12" ), // use incr
new SetMxFrAndRndIncrItem( "18 en_US DEC 1/3/3/0.02", "en_US", NumberFormat.NUMBERSTYLE, 1, 3, 3, 0.02, "#,##0.020", 0.128, "0.12" ), // use incr; expFmt != ICU4C
new SetMxFrAndRndIncrItem( "18 en_US DEC 1/3/3/0.02", "en_US", NumberFormat.NUMBERSTYLE, 1, 3, 3, 0.02, "#,##0.020", 0.128, "0.120" ), // use incr
new SetMxFrAndRndIncrItem( "20 en_US DEC 1/1/1/0.0075", "en_US", NumberFormat.NUMBERSTYLE, 1, 1, 1, 0.0075, "#,##0.0", 0.019, "0.0" ),
new SetMxFrAndRndIncrItem( "21 en_US DEC 1/1/2/0.0075", "en_US", NumberFormat.NUMBERSTYLE, 1, 1, 2, 0.0075, "#,##0.0075", 0.004, "0.0075" ), // use incr
@ -4134,6 +4134,9 @@ public class NumberFormatTest extends TestFmwk {
double testIncr = item.roundIncr;
for (; testIncr > ((int)testIncr); testIncr *= 10.0, fracForRoundIncr++);
}
if (fracForRoundIncr < item.minFrac) {
fracForRoundIncr = item.minFrac;
}
int minInt = df.getMinimumIntegerDigits();
if (minInt != item.minInt) {
@ -6907,6 +6910,22 @@ public class NumberFormatTest extends TestFmwk {
}
}
@Test
public void Test20425_IntegerIncrement() {
DecimalFormat df = new DecimalFormat("##00");
df.setRoundingIncrement(1);
String actual = df.format(1235.5);
assertEquals("Should round to integer", "1236", actual);
}
@Test
public void Test20425_FractionWithIntegerIncrement() {
DecimalFormat df = new DecimalFormat("0.0");
df.setRoundingIncrement(1);
String actual = df.format(8.6);
assertEquals("Should have a fraction digit", "9.0", actual);
}
@Test
public void Test21232_ParseTimeout() throws ParseException {
DecimalFormat df = new DecimalFormat();