]> granicus.if.org Git - icu/commitdiff
ICU-10601 Added UNUM_CURRENCY_ACCOUNTING to ICU4C NumberFormat.
authorYoshito Umaoka <y.umaoka@gmail.com>
Sat, 22 Feb 2014 00:16:56 +0000 (00:16 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Sat, 22 Feb 2014 00:16:56 +0000 (00:16 +0000)
X-SVN-Rev: 35198

icu4c/source/i18n/numfmt.cpp
icu4c/source/i18n/unicode/unum.h
icu4c/source/i18n/unum.cpp
icu4c/source/test/intltest/numfmtst.cpp
icu4c/source/test/intltest/numfmtst.h

index 95ea05c41b0045e604efa6ea106299798567b201..5806f0776ba03dae59eae920c1523998c862f047 100644 (file)
@@ -89,6 +89,9 @@ static const UChar gLastResortIsoCurrencyPat[] = {
 static const UChar gLastResortPluralCurrencyPat[] = {
     0x23, 0x30, 0x2E, 0x30, 0x30, 0xA0, 0xA4, 0xA4, 0xA4, 0 /* "#0.00\u00A0\u00A4\u00A4\u00A4*/
 };
+static const UChar gLastResortAccountingCurrencyPat[] = {
+    0xA4, 0x23, 0x30, 0x2E, 0x30, 0x30, 0x3B, 0x28, 0xA4, 0x23, 0x30, 0x2E, 0x30, 0x30, 0x29, 0 /* "\u00A4#0.00;(\u00A4#0.00)" */
+};
 
 static const UChar gSingleCurrencySign[] = {0xA4, 0};
 static const UChar gDoubleCurrencySign[] = {0xA4, 0xA4, 0};
@@ -114,7 +117,8 @@ static const UChar * const gLastResortNumberPatterns[UNUM_FORMAT_STYLE_COUNT] =
     NULL,  // UNUM_NUMBERING_SYSTEM
     NULL,  // UNUM_PATTERN_RULEBASED
     gLastResortIsoCurrencyPat,  // UNUM_CURRENCY_ISO
-    gLastResortPluralCurrencyPat  // UNUM_CURRENCY_PLURAL
+    gLastResortPluralCurrencyPat,  // UNUM_CURRENCY_PLURAL
+    gLastResortAccountingCurrencyPat // UNUM_CURRENCY_ACCOUNTING
 };
 
 // Keys used for accessing resource bundles
@@ -138,7 +142,8 @@ static const char *gFormatKeys[UNUM_FORMAT_STYLE_COUNT] = {
     // except for replacing the single currency sign with
     // double currency sign or triple currency sign.
     "currencyFormat",  // UNUM_CURRENCY_ISO
-    "currencyFormat"  // UNUM_CURRENCY_PLURAL
+    "currencyFormat",  // UNUM_CURRENCY_PLURAL
+    "accountingFormat"  // UNUM_CURRENCY_ACCOUNTING
 };
 
 static icu::LRUCache *gNumberFormatCache = NULL;
@@ -1344,6 +1349,7 @@ NumberFormat::makeInstance(const Locale& desiredLocale,
             case UNUM_CURRENCY:
             case UNUM_CURRENCY_ISO: // do not support plural formatting here
             case UNUM_CURRENCY_PLURAL:
+            case UNUM_CURRENCY_ACCOUNTING:
                 f = new Win32NumberFormat(desiredLocale, curr, status);
 
                 if (U_SUCCESS(status)) {
@@ -1438,7 +1444,7 @@ NumberFormat::makeInstance(const Locale& desiredLocale,
     if (U_FAILURE(status)) {
         return NULL;
     }
-    if(style==UNUM_CURRENCY || style == UNUM_CURRENCY_ISO){
+    if(style==UNUM_CURRENCY || style == UNUM_CURRENCY_ISO || style == UNUM_CURRENCY_ACCOUNTING){
         const UChar* currPattern = symbolsToAdopt->getCurrencyPattern();
         if(currPattern!=NULL){
             pattern.setTo(currPattern, u_strlen(currPattern));
index b15efb2d80699671c4d6619465f42571514acd9d..7100a69f521129301a81574f567ef5e917c8111b 100644 (file)
@@ -197,6 +197,12 @@ typedef enum UNumberFormatStyle {
      * @stable ICU 4.8
      */
     UNUM_CURRENCY_PLURAL,
+    /**
+     * Currency format for accounting, e.g., "($3.00)" for
+     * negative currency amount instead of "-$3.00" ({@link #UNUM_CURRENCY}).
+     * @draft ICU 53
+     */
+    UNUM_CURRENCY_ACCOUNTING,
     /**
      * One more than the highest number format style constant.
      * @stable ICU 4.8
index f58ce4b49be6cf7c3450fe2c1c086ad1d3f8f67e..5d606129de8671c9801d79b3a0f5771cb29517ee 100644 (file)
@@ -54,6 +54,7 @@ unum_open(  UNumberFormatStyle    style,
     case UNUM_SCIENTIFIC:
     case UNUM_CURRENCY_ISO:
     case UNUM_CURRENCY_PLURAL:
+    case UNUM_CURRENCY_ACCOUNTING:
         retVal = NumberFormat::createInstance(Locale(locale), style, *status);
         break;
 
index 09546425c59d04e03932fe4c8a67c5d2ada0dd66..5c5c401355445c438859407f9cecef1d188bb056 100644 (file)
@@ -132,6 +132,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n
   TESTCASE_AUTO(Test10468ApplyPattern);
   TESTCASE_AUTO(TestRoundingScientific10542);
   TESTCASE_AUTO(TestZeroScientific10547);
+  TESTCASE_AUTO(TestAccountingCurrency);
   TESTCASE_AUTO_END;
 }
 
@@ -2549,12 +2550,12 @@ void NumberFormatTest::expect(NumberFormat& fmt, const Formattable& n,
 }
 
 void NumberFormatTest::expect(NumberFormat* fmt, const Formattable& n,
-                              const UnicodeString& exp,
+                              const UnicodeString& exp, UBool rt,
                               UErrorCode status) {
     if (fmt == NULL || U_FAILURE(status)) {
         dataerrln("FAIL: NumberFormat constructor");
     } else {
-        expect(*fmt, n, exp);
+        expect(*fmt, n, exp, rt);
     }
     delete fmt;
 }
@@ -7579,4 +7580,24 @@ void NumberFormatTest::verifyRounding(
     }
 }
 
+void NumberFormatTest::TestAccountingCurrency() {
+    UErrorCode status = U_ZERO_ERROR;
+    UNumberFormatStyle style = UNUM_CURRENCY_ACCOUNTING;
+
+    expect(NumberFormat::createInstance("en_US", style, status),
+        (Formattable)1234.5, "$1,234.50", TRUE, status);
+    expect(NumberFormat::createInstance("en_US", style, status),
+        (Formattable)-1234.5, "($1,234.50)", TRUE, status);
+    expect(NumberFormat::createInstance("en_US", style, status),
+        (Formattable)0, "$0.00", TRUE, status);
+    expect(NumberFormat::createInstance("en_US", style, status),
+        (Formattable)-0.2, "($0.20)", TRUE, status);
+    expect(NumberFormat::createInstance("ja_JP", style, status),
+        (Formattable)10000, UnicodeString("\\uFFE510,000").unescape(), TRUE, status);
+    expect(NumberFormat::createInstance("ja_JP", style, status),
+        (Formattable)-1000.5, UnicodeString("(\\uFFE51,000)").unescape(), FALSE, status);
+    expect(NumberFormat::createInstance("de_DE", style, status),
+        (Formattable)-23456.7, UnicodeString("-23.456,70\\u00A0\\u20AC").unescape(), TRUE, status);
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */
index f220f20c708a712f07882a9f6a3521597adfafa5..171995fb7c2591a89f37cad15536790c1b200947 100644 (file)
@@ -179,6 +179,7 @@ class NumberFormatTest: public CalendarTimeZoneTest {
     void Test10468ApplyPattern();
     void TestRoundingScientific10542();
     void TestZeroScientific10547();
+    void TestAccountingCurrency();
 
  private:
     UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f);
@@ -222,11 +223,21 @@ class NumberFormatTest: public CalendarTimeZoneTest {
     }
 
     void expect(NumberFormat* fmt, const Formattable& n,
-                const UnicodeString& exp, UErrorCode);
+                const UnicodeString& exp, UBool rt, UErrorCode errorCode);
+
+    void expect(NumberFormat* fmt, const Formattable& n,
+                const char *exp, UBool rt, UErrorCode errorCode) {
+        expect(fmt, n, UnicodeString(exp, ""), rt, errorCode);
+    }
+
+    void expect(NumberFormat* fmt, const Formattable& n,
+                const UnicodeString& exp, UErrorCode errorCode) {
+        expect(fmt, n, exp, TRUE, errorCode);
+    }
 
     void expect(NumberFormat* fmt, const Formattable& n,
                 const char *exp, UErrorCode errorCode) {
-        expect(fmt, n, UnicodeString(exp, ""), errorCode);
+        expect(fmt, n, UnicodeString(exp, ""), TRUE, errorCode);
     }
 
     void expectCurrency(NumberFormat& nf, const Locale& locale,