]> granicus.if.org Git - icu/commitdiff
ICU-13060 Minor tweak to CurrencyUsage override; allows CurrencyUsage without a curre...
authorShane Carr <shane@unicode.org>
Wed, 29 Mar 2017 06:33:41 +0000 (06:33 +0000)
committerShane Carr <shane@unicode.org>
Wed, 29 Mar 2017 06:33:41 +0000 (06:33 +0000)
X-SVN-Rev: 39954

icu4j/main/classes/core/src/com/ibm/icu/impl/number/formatters/CurrencyFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index 7090ba6fc855e8c28ac0a0f8f6e4ebfb176ab7de..8d499678496e6fdbf03e12bd11943cdf7dd02cee 100644 (file)
@@ -107,6 +107,7 @@ public class CurrencyFormat {
   public static boolean useCurrency(IProperties properties) {
     return ((properties.getCurrency() != null)
         || properties.getCurrencyPluralInfo() != null
+        || properties.getCurrencyUsage() != null
         || AffixPatternUtils.hasCurrencySymbols(properties.getPositivePrefixPattern())
         || AffixPatternUtils.hasCurrencySymbols(properties.getPositiveSuffixPattern())
         || AffixPatternUtils.hasCurrencySymbols(properties.getNegativePrefixPattern())
@@ -242,25 +243,25 @@ public class CurrencyFormat {
       currency = DEFAULT_CURRENCY;
     }
 
-    Currency.CurrencyUsage currencyUsage = properties.getCurrencyUsage();
-    if (currencyUsage == null) {
-      currencyUsage = CurrencyUsage.STANDARD;
-    }
+    CurrencyUsage _currencyUsage = properties.getCurrencyUsage();
+    int _minFrac = properties.getMinimumFractionDigits();
+    int _maxFrac = properties.getMaximumFractionDigits();
 
-    double incrementDouble = currency.getRoundingIncrement(currencyUsage);
-    int fractionDigits = currency.getDefaultFractionDigits(currencyUsage);
+    CurrencyUsage effectiveCurrencyUsage =
+        (_currencyUsage != null) ? _currencyUsage : CurrencyUsage.STANDARD;
+    double incrementDouble = currency.getRoundingIncrement(effectiveCurrencyUsage);
+    int fractionDigits = currency.getDefaultFractionDigits(effectiveCurrencyUsage);
 
     destination.setRoundingMode(properties.getRoundingMode());
     destination.setMinimumIntegerDigits(properties.getMinimumIntegerDigits());
     destination.setMaximumIntegerDigits(properties.getMaximumIntegerDigits());
 
-    int _minFrac = properties.getMinimumFractionDigits();
-    int _maxFrac = properties.getMaximumFractionDigits();
-    if (_minFrac >= 0 || _maxFrac >= 0) {
-      // User override
+    if (_currencyUsage == null && (_minFrac >= 0 || _maxFrac >= 0)) {
+      // User override of fraction length
       destination.setMinimumFractionDigits(_minFrac);
       destination.setMaximumFractionDigits(_maxFrac);
     } else {
+      // Currency rounding
       destination.setMinimumFractionDigits(fractionDigits);
       destination.setMaximumFractionDigits(fractionDigits);
     }
index 1ad5884c5cdaeb61f5170b4181de8bd113be8e29..ec4168a90b6516070edcf550fd5fe91668071727 100644 (file)
@@ -5192,6 +5192,16 @@ public class NumberFormatTest extends TestFmwk {
         assertEquals("Should not consume the trailing bidi or whitespace", 4, ppos.getIndex());
     }
 
+    @Test
+    public void testCustomCurrencyUsageOverridesPattern() {
+        DecimalFormat df = new DecimalFormat("#,##0.###");
+        expect2(df, 1234, "1,234");
+        df.setCurrencyUsage(CurrencyUsage.STANDARD);
+        expect2(df, 1234, "1,234.00");
+        df.setCurrencyUsage(null);
+        expect2(df, 1234, "1,234");
+    }
+
     @Test
     public void testSignificantDigitsMode() {
         String[][] allExpected = {