// Add the fraction digits
length += writeFractionDigits(micros, quantity, string, length + index);
+
+ if (length == 0) {
+ // Force output of the digit for value 0
+ if (micros.symbols.getCodePointZero() != -1) {
+ length += string.insertCodePoint(index,
+ micros.symbols.getCodePointZero(),
+ NumberFormat.Field.INTEGER);
+ } else {
+ length += string.insert(index,
+ micros.symbols.getDigitStringsLocal()[0],
+ NumberFormat.Field.INTEGER);
+ }
+ }
}
return length;
}
// Validate min/max int/frac.
// For backwards compatibility, minimum overrides maximum if the two conflict.
- // The following logic ensures that there is always a minimum of at least one digit.
if (minInt == 0 && maxFrac != 0) {
- // Force a digit after the decimal point.
- minFrac = minFrac <= 0 ? 1 : minFrac;
+ minFrac = (minFrac < 0 || (minFrac == 0 && maxInt == 0)) ? 1 : minFrac;
maxFrac = maxFrac < 0 ? -1 : maxFrac < minFrac ? minFrac : maxFrac;
minInt = 0;
maxInt = maxInt < 0 ? -1 : maxInt > RoundingUtils.MAX_INT_FRAC_SIG ? -1 : maxInt;
}
}
+ @Test
+ public void TestMinIntMinFracZero() {
+ class TestMinIntMinFracItem {
+ double value;;
+ String expDecFmt;
+ String expCurFmt;
+ // Simple constructor
+ public TestMinIntMinFracItem(double valueIn, String expDecFmtIn, String expCurFmtIn) {
+ value = valueIn;
+ expDecFmt = expDecFmtIn;
+ expCurFmt = expCurFmtIn;
+ }
+ };
+
+ final TestMinIntMinFracItem[] items = {
+ // decFmt curFmt
+ new TestMinIntMinFracItem( 10.0, "10", "$10" ),
+ new TestMinIntMinFracItem( 0.9, ".9", "$.9" ),
+ new TestMinIntMinFracItem( 0.0, "0", "$0" ),
+ };
+ int minInt, minFrac;
+
+ NumberFormat decFormat = NumberFormat.getInstance(ULocale.US, NumberFormat.NUMBERSTYLE);
+ decFormat.setMinimumIntegerDigits(0);
+ decFormat.setMinimumFractionDigits(0);
+ minInt = decFormat.getMinimumIntegerDigits();
+ minFrac = decFormat.getMinimumFractionDigits();
+ if (minInt != 0 || minFrac != 0) {
+ errln("after setting DECIMAL minInt=minFrac=0, get minInt " + minInt + ", minFrac " + minFrac);
+ }
+ String decPattern = ((DecimalFormat)decFormat).toPattern();
+ if (decPattern.length() < 3 || decPattern.indexOf("#.#")< 0) {
+ errln("after setting DECIMAL minInt=minFrac=0, expect pattern to contain \"#.#\", but get " + decPattern);
+ }
+
+ NumberFormat curFormat = NumberFormat.getInstance(ULocale.US, NumberFormat.CURRENCYSTYLE);
+ curFormat.setMinimumIntegerDigits(0);
+ curFormat.setMinimumFractionDigits(0);
+ minInt = curFormat.getMinimumIntegerDigits();
+ minFrac = curFormat.getMinimumFractionDigits();
+ if (minInt != 0 || minFrac != 0) {
+ errln("after setting CURRENCY minInt=minFrac=0, get minInt " + minInt + ", minFrac " + minFrac);
+ }
+
+ for (TestMinIntMinFracItem item: items) {
+ String decString = decFormat.format(item.value);
+ if (!decString.equals(item.expDecFmt)) {
+ errln("format DECIMAL value " + item.value + ", expected \"" + item.expDecFmt + "\", got \"" + decString + "\"");
+ }
+ String curString = curFormat.format(item.value);
+ if (!curString.equals(item.expCurFmt)) {
+ errln("format CURRENCY value " + item.value + ", expected \"" + item.expCurFmt + "\", got \"" + curString + "\"");
+ }
+ }
+ }
+
@Test
public void TestBug9936() {
DecimalFormat numberFormat =