]> granicus.if.org Git - icu/commitdiff
ICU-13293 RuleBasedNumberFormat should not throw an exception when mixing rounding...
authorGeorge Rhoten <grhoten@users.noreply.github.com>
Thu, 17 Aug 2017 23:25:38 +0000 (23:25 +0000)
committerGeorge Rhoten <grhoten@users.noreply.github.com>
Thu, 17 Aug 2017 23:25:38 +0000 (23:25 +0000)
X-SVN-Rev: 40344

icu4j/main/classes/core/src/com/ibm/icu/text/RuleBasedNumberFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfTest.java

index d54c16cd9c97cac380763e9cb8291cd6742db03a..05b4498ea1d3439672150e3ba6f0461cabe1e357 100644 (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();
         }
index c82ab7c95e15f18eef8c2459285aee2d53c64e48..ec80686c447ce1e55387f180c730ae4621f74b22 100644 (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));
+    }
 }