// 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();
}
};
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));
+ }
}