ICU-13293 RuleBasedNumberFormat should not throw an exception when mixing rounding and unreal numbers

X-SVN-Rev: 40344
This commit is contained in:
George Rhoten 2017-08-17 23:25:38 +00:00
parent e3ac9c5561
commit 221c076ac2
2 changed files with 18 additions and 1 deletions

View File

@ -1952,7 +1952,7 @@ public class RuleBasedNumberFormat extends NumberFormat {
// position of 0 and the number being formatted) to the rule set
// for formatting
StringBuilder result = new StringBuilder();
if (getRoundingMode() != BigDecimal.ROUND_UNNECESSARY) {
if (getRoundingMode() != BigDecimal.ROUND_UNNECESSARY && !Double.isNaN(number) && !Double.isInfinite(number)) {
// We convert to a string because BigDecimal insists on excessive precision.
number = new BigDecimal(Double.toString(number)).setScale(getMaximumFractionDigits(), roundingMode).doubleValue();
}

View File

@ -1705,4 +1705,21 @@ public class RbnfTest extends TestFmwk {
};
doTest(rbnf, enTestFullData, false);
}
private void assertEquals(String expected, String result) {
if (!expected.equals(result)) {
errln("Expected: " + expected + " Got: " + result);
}
}
@Test
public void testRoundingUnrealNumbers() {
RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(ULocale.US, RuleBasedNumberFormat.SPELLOUT);
rbnf.setRoundingMode(BigDecimal.ROUND_HALF_UP);
rbnf.setMaximumFractionDigits(3);
assertEquals("zero point one", rbnf.format(0.1));
assertEquals("zero point zero zero one", rbnf.format(0.0005));
assertEquals("infinity", rbnf.format(Double.POSITIVE_INFINITY));
assertEquals("not a number", rbnf.format(Double.NaN));
}
}