import com.ibm.icu.impl.ICUResourceBundle;
import com.ibm.icu.impl.PatternProps;
import com.ibm.icu.lang.UCharacter;
+import com.ibm.icu.math.BigDecimal;
import com.ibm.icu.util.ULocale;
import com.ibm.icu.util.ULocale.Category;
import com.ibm.icu.util.UResourceBundle;
*/
private ULocale locale = null;
+ /**
+ * The formatter's rounding mode.
+ * @serial
+ */
+ private int roundingMode = BigDecimal.ROUND_UNNECESSARY;
+
/**
* Collator to be used in lenient parsing. This variable is lazy-evaluated:
* the collator is actually created the first time the client does a parse
// have an implementation-independent streaming format
out.writeUTF(this.toString());
out.writeObject(this.locale);
+ out.writeInt(this.roundingMode);
}
/**
} catch (Exception e) {
loc = ULocale.getDefault(Category.FORMAT);
}
+ try {
+ roundingMode = in.readInt();
+ } catch (Exception ignored) {
+ }
// build a brand-new RuleBasedNumberFormat from the description,
// then steal its substructure. This object's substructure and
decimalFormatSymbols = temp.decimalFormatSymbols;
decimalFormat = temp.decimalFormat;
locale = temp.locale;
+ defaultInfinityRule = temp.defaultInfinityRule;
+ defaultNaNRule = temp.defaultNaNRule;
}
}
}
+ /**
+ * Returns the rounding mode.
+ *
+ * @return A rounding mode, between <code>BigDecimal.ROUND_UP</code> and
+ * <code>BigDecimal.ROUND_UNNECESSARY</code>.
+ * @see #setRoundingMode
+ * @see java.math.BigDecimal
+ * @draft ICU 56
+ */
+ @Override
+ public int getRoundingMode() {
+ return roundingMode;
+ }
+
+ /**
+ * Sets the rounding mode. This has no effect unless the rounding increment is greater
+ * than zero.
+ *
+ * @param roundingMode A rounding mode, between <code>BigDecimal.ROUND_UP</code> and
+ * <code>BigDecimal.ROUND_UNNECESSARY</code>.
+ * @exception IllegalArgumentException if <code>roundingMode</code> is unrecognized.
+ * @see #getRoundingMode
+ * @see java.math.BigDecimal
+ * @draft ICU 56
+ */
+ @Override
+ public void setRoundingMode(int roundingMode) {
+ if (roundingMode < BigDecimal.ROUND_UP || roundingMode > BigDecimal.ROUND_UNNECESSARY) {
+ throw new IllegalArgumentException("Invalid rounding mode: " + roundingMode);
+ }
+
+ this.roundingMode = roundingMode;
+ }
+
+
//-----------------------------------------------------------------------
// package-internal API
//-----------------------------------------------------------------------
// position of 0 and the number being formatted) to the rule set
// for formatting
StringBuffer result = new StringBuffer();
+ if (getRoundingMode() != BigDecimal.ROUND_UNNECESSARY) {
+ number = new BigDecimal(number).setScale(getMaximumFractionDigits(), roundingMode).doubleValue();
+ }
ruleSet.format(number, result, 0, 0);
postProcess(result, ruleSet);
return result.toString();
import java.util.Random;
import com.ibm.icu.dev.test.TestFmwk;
+import com.ibm.icu.math.BigDecimal;
import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.DecimalFormatSymbols;
import com.ibm.icu.text.DisplayContext;
String[] ruleNames = rbnf.getRuleSetNames();
try{
// Test with "null" rules
- rbnf.format(0.0,null);
+ rbnf.format(0.0, null);
errln("This was suppose to return an exception for a null format");
} catch(Exception e){}
for(int i=0; i<ruleNames.length; i++){
doTest(enFormatter, enTestCommaData, true);
}
+ public void TestRounding() {
+ RuleBasedNumberFormat enFormatter = new RuleBasedNumberFormat(ULocale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
+ String[][] enTestPointData = {
+ {"0", "zero"},
+ {"0.4", "zero point four"},
+ {"0.49", "zero point four nine"},
+ {"0.5", "zero point five"},
+ {"0.51", "zero point five one"},
+ {"0.99", "zero point nine nine"},
+ {"1", "one"},
+ {"1.01", "one point zero one"},
+ {"1.49", "one point four nine"},
+ {"1.5", "one point five"},
+ {"1.51", "one point five one"},
+ };
+ doTest(enFormatter, enTestPointData, false);
+
+ enFormatter.setMaximumFractionDigits(0);
+ enFormatter.setRoundingMode(BigDecimal.ROUND_HALF_EVEN);
+ String[][] enTestCommaData = {
+ {"0", "zero"},
+ {"0.4", "zero"},
+ {"0.49", "zero"},
+ {"0.5", "zero"},
+ {"0.51", "one"},
+ {"0.99", "one"},
+ {"1", "one"},
+ {"1.01", "one"},
+ {"1.49", "one"},
+ {"1.5", "two"},
+ {"1.51", "two"},
+ };
+ doTest(enFormatter, enTestCommaData, false);
+ }
}