From: Peter Edberg Date: Tue, 10 Mar 2020 03:44:43 +0000 (-0700) Subject: ICU-20844 ICU4J, reduce restriction on minInt=minFrac=0 X-Git-Tag: release-67-rc~64 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=547030b8da78511c08f180066a2e3e9b8353faa3;p=icu ICU-20844 ICU4J, reduce restriction on minInt=minFrac=0 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java index 48607c051a6..a84968533dd 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java @@ -444,6 +444,19 @@ class NumberFormatterImpl { // 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; diff --git a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberPropertyMapper.java b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberPropertyMapper.java index 3a5fcdcda0c..afe9679aaaa 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/number/NumberPropertyMapper.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/number/NumberPropertyMapper.java @@ -155,10 +155,8 @@ final class NumberPropertyMapper { } // 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; diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index e555bb6887a..42034df573c 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -4166,6 +4166,62 @@ public class NumberFormatTest extends TestFmwk { } } + @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 = diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java index 2512b1fc7aa..54c1d98e630 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java @@ -1737,7 +1737,7 @@ public class NumberFormatterApiTest { ".8765", ".08765", ".008765", - ""); // TODO: Avoid the empty string here? + "0"); // see ICU-20844 assertFormatDescending( "Integer Width Zero Fill 3",