]> granicus.if.org Git - icu/commitdiff
ICU-10601 Added ACCOUNTINGCURRENCYSTYLE to NumberFormat.
authorYoshito Umaoka <y.umaoka@gmail.com>
Fri, 21 Feb 2014 19:15:04 +0000 (19:15 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Fri, 21 Feb 2014 19:15:04 +0000 (19:15 +0000)
X-SVN-Rev: 35194

icu4j/main/classes/core/src/com/ibm/icu/text/NumberFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index 4a5f68260af7627e494c1be3f8a53f9eedea0f47..5d4134970e1ad4afec13eedd007b72027cb0fb59 100644 (file)
@@ -207,6 +207,14 @@ public abstract class NumberFormat extends UFormat {
      * @stable ICU 4.2
      */
     public static final int PLURALCURRENCYSTYLE = 6;
+    /**
+     * {@icu} Constant to specify currency style of format which uses currency symbol
+     * to represent currency for accounting, for example: "($3.00), instead of
+     * "-$3.00" ({@link #CURRENCYSTYLE}).
+     * @draft ICU 53
+     * @provisional This API might change or be removed in a future release.
+     */
+    public static final int ACCOUNTINGCURRENCYSTYLE = 7;
 
     /**
      * Field constant used to construct a FieldPosition object. Signifies that
@@ -1283,12 +1291,12 @@ public abstract class NumberFormat extends UFormat {
      * @throws IllegalArgumentException  if choice is not one of
      *                                   NUMBERSTYLE, CURRENCYSTYLE,
      *                                   PERCENTSTYLE, SCIENTIFICSTYLE,
-     *                                   INTEGERSTYLE,
-     *                                   ISOCURRENCYSTYLE, PLURALCURRENCYSTYLE,
+     *                                   INTEGERSTYLE, ISOCURRENCYSTYLE,
+     *                                   PLURALCURRENCYSTYLE and ACCOUNTSTYLE.
      * @stable ICU 4.2
      */
     public static NumberFormat getInstance(ULocale desiredLocale, int choice) {
-        if (choice < NUMBERSTYLE || choice > PLURALCURRENCYSTYLE) {
+        if (choice < NUMBERSTYLE || choice > ACCOUNTINGCURRENCYSTYLE) {
             throw new IllegalArgumentException(
                 "choice should be from NUMBERSTYLE to PLURALCURRENCYSTYLE");
         }
@@ -1317,7 +1325,7 @@ public abstract class NumberFormat extends UFormat {
         // This style wont work for currency plural format.
         // For currency plural format, the pattern is get from
         // the locale (from CurrencyUnitPatterns) without override.
-        if(choice == CURRENCYSTYLE || choice == ISOCURRENCYSTYLE){
+        if(choice == CURRENCYSTYLE || choice == ISOCURRENCYSTYLE || choice == ACCOUNTINGCURRENCYSTYLE){
             String temp = symbols.getCurrencyPattern();
             if(temp!=null){
                 pattern = temp;
@@ -1456,20 +1464,41 @@ public abstract class NumberFormat extends UFormat {
          * but by replacing the single currency sign with
          * double currency sign or triple currency sign.
          */
-        int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE :
-                ((choice == ISOCURRENCYSTYLE || choice == PLURALCURRENCYSTYLE)?
-                CURRENCYSTYLE : choice); //[Richard/GCL]
+        String patternKey = null;
+        switch (choice) {
+        case NUMBERSTYLE:
+        case INTEGERSTYLE:
+            patternKey = "decimalFormat";
+            break;
+        case CURRENCYSTYLE:
+        case ISOCURRENCYSTYLE:
+        case PLURALCURRENCYSTYLE:
+            patternKey = "currencyFormat";
+            break;
+        case PERCENTSTYLE:
+            patternKey = "percentFormat";
+            break;
+        case SCIENTIFICSTYLE:
+            patternKey = "scientificFormat";
+            break;
+        case ACCOUNTINGCURRENCYSTYLE:
+            patternKey = "accountingFormat";
+            break;
+        default:
+            assert false;
+            patternKey = "decimalFormat";
+            break;
+        }
 
         ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.
         getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, forLocale);
-        String[] numberPatternKeys = { "decimalFormat", "currencyFormat", "percentFormat", "scientificFormat" };
         NumberingSystem ns = NumberingSystem.getInstance(forLocale);
 
         String result = null;
         try {
-            result = rb.getStringWithFallback("NumberElements/" + ns.getName() + "/patterns/"+numberPatternKeys[entry]);
+            result = rb.getStringWithFallback("NumberElements/" + ns.getName() + "/patterns/"+ patternKey);
         } catch ( MissingResourceException ex ) {
-            result = rb.getStringWithFallback("NumberElements/latn/patterns/"+numberPatternKeys[entry]);
+            result = rb.getStringWithFallback("NumberElements/latn/patterns/"+ patternKey);
         }
 
         return result;
index 611281e2cb7c4d2ea8cc2efaf17d9a7b2d246ef4..ed7bee53026677656658da138cf80dbdf29b91d2 100644 (file)
@@ -2733,11 +2733,12 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
      */
     public void TestGetInstance() {
         // Tests "public final static NumberFormat getInstance(int style)"
+        int maxStyle = NumberFormat.ACCOUNTINGCURRENCYSTYLE;
 
         int[] invalid_cases = { NumberFormat.NUMBERSTYLE - 1, NumberFormat.NUMBERSTYLE - 2,
-                NumberFormat.PLURALCURRENCYSTYLE + 1, NumberFormat.PLURALCURRENCYSTYLE + 2 };
+                maxStyle + 1, maxStyle + 2 };
 
-        for (int i = NumberFormat.NUMBERSTYLE; i < NumberFormat.PLURALCURRENCYSTYLE; i++) {
+        for (int i = NumberFormat.NUMBERSTYLE; i < maxStyle; i++) {
             try {
                 NumberFormat.getInstance(i);
             } catch (Exception e) {
@@ -2758,7 +2759,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
         // Tests "public static NumberFormat getInstance(Locale inLocale, int style)"
         String[] localeCases = { "en_US", "fr_FR", "de_DE", "jp_JP" };
 
-        for (int i = NumberFormat.NUMBERSTYLE; i < NumberFormat.PLURALCURRENCYSTYLE; i++) {
+        for (int i = NumberFormat.NUMBERSTYLE; i < maxStyle; i++) {
             for (int j = 0; j < localeCases.length; j++) {
                 try {
                     NumberFormat.getInstance(new Locale(localeCases[j]), i);
@@ -3584,4 +3585,25 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
             errln("FAIL: NumberFormat.getContext() does not return the value set, CAPITALIZATION_FOR_STANDALONE");
         }
     }
+
+    public void TestAccountingCurrency() {
+        String[][] tests = {
+                {"en_US", "1234.5", "$1,234.50", "true"},
+                {"en_US", "-1234.5", "($1,234.50)", "true"},
+                {"en_US", "0", "$0.00", "true"},
+                {"en_US", "-0.2", "($0.20)", "true"},
+                {"ja_JP", "10000", "¥10,000", "true"},
+                {"ja_JP", "-1000.5", "(¥1,000)", "false"},
+                {"de_DE", "-23456.7", "-23.456,70\u00A0€", "true"},
+        };
+        for (String[] data : tests) {
+            ULocale loc = new ULocale(data[0]);
+            double num = Double.parseDouble(data[1]);
+            String fmt = data[2];
+            boolean rt = Boolean.parseBoolean(data[3]);
+
+            NumberFormat acfmt = NumberFormat.getInstance(loc, NumberFormat.ACCOUNTINGCURRENCYSTYLE);
+            expect(acfmt, num, fmt, rt);
+        }
+    }
 }