From cfd57abf79981e282a82e28f1be592e7a2630642 Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Sat, 22 Feb 2014 00:16:56 +0000 Subject: [PATCH] ICU-10601 Added UNUM_CURRENCY_ACCOUNTING to ICU4C NumberFormat. X-SVN-Rev: 35198 --- icu4c/source/i18n/numfmt.cpp | 12 +++++++++--- icu4c/source/i18n/unicode/unum.h | 6 ++++++ icu4c/source/i18n/unum.cpp | 1 + icu4c/source/test/intltest/numfmtst.cpp | 25 +++++++++++++++++++++++-- icu4c/source/test/intltest/numfmtst.h | 15 +++++++++++++-- 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/icu4c/source/i18n/numfmt.cpp b/icu4c/source/i18n/numfmt.cpp index 95ea05c41b0..5806f0776ba 100644 --- a/icu4c/source/i18n/numfmt.cpp +++ b/icu4c/source/i18n/numfmt.cpp @@ -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)); diff --git a/icu4c/source/i18n/unicode/unum.h b/icu4c/source/i18n/unicode/unum.h index b15efb2d806..7100a69f521 100644 --- a/icu4c/source/i18n/unicode/unum.h +++ b/icu4c/source/i18n/unicode/unum.h @@ -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 diff --git a/icu4c/source/i18n/unum.cpp b/icu4c/source/i18n/unum.cpp index f58ce4b49be..5d606129de8 100644 --- a/icu4c/source/i18n/unum.cpp +++ b/icu4c/source/i18n/unum.cpp @@ -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; diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 09546425c59..5c5c4013554 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -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 */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index f220f20c708..171995fb7c2 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -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, -- 2.40.0