diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java index d54c16cd9c..05b4498ea1 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java @@ -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(); } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java index c82ab7c95e..ec80686c44 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java @@ -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)); + } }