]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/numfmt/main.cpp
1 /********************************************************************
3 * Copyright (c) 1999-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
8 #include "unicode/unistr.h"
9 #include "unicode/numfmt.h"
10 #include "unicode/dcfmtsym.h"
11 #include "unicode/decimfmt.h"
12 #include "unicode/locid.h"
13 #include "unicode/uclean.h"
19 extern "C" void capi();
23 showCurrencyFormatting(UBool useICU26API
);
25 int main(int argc
, char **argv
) {
26 printf("%s output is in UTF-8\n", argv
[0]);
34 showCurrencyFormatting(FALSE
);
35 showCurrencyFormatting(TRUE
);
37 u_cleanup(); // Release any additional storage held by ICU.
39 printf("Exiting successfully\n");
44 * Sample code for the C++ API to NumberFormat.
47 Locale
us("en", "US");
48 UErrorCode status
= U_ZERO_ERROR
;
50 // Create a number formatter for the US locale
51 NumberFormat
*fmt
= NumberFormat::createInstance(us
, status
);
52 check(status
, "NumberFormat::createInstance");
54 // Parse a string. The string uses the digits '0' through '9'
55 // and the decimal separator '.', standard in the US locale
56 UnicodeString
str("9876543210.123");
58 fmt
->parse(str
, result
, status
);
59 check(status
, "NumberFormat::parse");
61 printf("NumberFormat::parse(\""); // Display the result
64 uprintf(formattableToString(result
));
67 // Take the number parsed above, and use the formatter to
69 str
.remove(); // format() will APPEND to this string
70 fmt
->format(result
, str
, status
);
71 check(status
, "NumberFormat::format");
73 printf("NumberFormat::format("); // Display the result
74 uprintf(formattableToString(result
));
79 delete fmt
; // Release the storage used by the formatter
83 // currency formatting ----------------------------------------------------- ***
86 * Set a currency on a NumberFormat with pre-ICU 2.6 APIs.
87 * This is a "hack" that will not work properly for all cases because
88 * only ICU 2.6 introduced a more complete framework and data for this.
90 * @param nf The NumberFormat on which to set the currency; takes effect on
91 * currency-formatting NumberFormat instances.
92 * This must actually be a DecimalFormat instance.
93 * The display style of the output is controlled by nf (its pattern,
94 * usually from the display locale ID used to create this instance)
95 * while the currency symbol and number of decimals are set for
97 * @param currency The 3-letter ISO 4217 currency code, NUL-terminated.
98 * @param errorCode ICU error code, must pass U_SUCCESS() on input.
101 setNumberFormatCurrency_2_4(NumberFormat
&nf
, const char *currency
, UErrorCode
&errorCode
) {
103 if(U_FAILURE(errorCode
)) {
106 if(currency
==NULL
|| strlen(currency
)!=3) {
107 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
111 // check that the formatter is a DecimalFormat instance
112 // necessary because we will cast to the DecimalFormat subclass to set
113 // the currency symbol
114 DecimalFormat
*dnf
=dynamic_cast<DecimalFormat
*>(&nf
);
116 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
120 // map the currency code to a locale ID
121 // only the currencies in this array are supported
122 // it would be possible to map to a locale ID, instantiate a currency
123 // formatter for that and copy its values, but that would be slower,
124 // and we have to hardcode something here anyway
125 static const struct {
127 const char *currency
;
129 // fractionDigits==minimumFractionDigits==maximumFractionDigits
130 // for these currencies
131 int32_t fractionDigits
;
134 * Set the rounding increment to 0 if it is implied with the number of
135 * fraction digits. Setting an explicit rounding increment makes
136 * number formatting slower.
137 * In other words, set it to something other than 0 only for unusual
138 * cases like "nickel rounding" (0.05) when the increment differs from
139 * 10^(-maximumFractionDigits).
141 double roundingIncrement
;
143 // Unicode string with the desired currency display symbol or name
146 { "USD", 2, 0.0, { 0x24, 0 } },
147 { "GBP", 2, 0.0, { 0xa3, 0 } },
148 { "EUR", 2, 0.0, { 0x20ac, 0 } },
149 { "JPY", 0, 0.0, { 0xa5, 0 } }
154 for(i
=0; i
<UPRV_LENGTHOF(currencyMap
); ++i
) {
155 if(strcmp(currency
, currencyMap
[i
].currency
)==0) {
159 if(i
==UPRV_LENGTHOF(currencyMap
)) {
160 // a more specific error code would be useful in a real application
161 errorCode
=U_UNSUPPORTED_ERROR
;
165 // set the currency-related data into the caller's formatter
167 nf
.setMinimumFractionDigits(currencyMap
[i
].fractionDigits
);
168 nf
.setMaximumFractionDigits(currencyMap
[i
].fractionDigits
);
170 dnf
->setRoundingIncrement(currencyMap
[i
].roundingIncrement
);
172 DecimalFormatSymbols
symbols(*dnf
->getDecimalFormatSymbols());
173 symbols
.setSymbol(DecimalFormatSymbols::kCurrencySymbol
, currencyMap
[i
].symbol
);
174 dnf
->setDecimalFormatSymbols(symbols
); // do not adopt symbols: Jitterbug 2889
178 * Set a currency on a NumberFormat with ICU 2.6 APIs.
180 * @param nf The NumberFormat on which to set the currency; takes effect on
181 * currency-formatting NumberFormat instances.
182 * The display style of the output is controlled by nf (its pattern,
183 * usually from the display locale ID used to create this instance)
184 * while the currency symbol and number of decimals are set for
186 * @param currency The 3-letter ISO 4217 currency code, NUL-terminated.
187 * @param errorCode ICU error code, must pass U_SUCCESS() on input.
190 setNumberFormatCurrency_2_6(NumberFormat
&nf
, const char *currency
, UErrorCode
&errorCode
) {
191 if(U_FAILURE(errorCode
)) {
194 if(currency
==NULL
|| strlen(currency
)!=3) {
195 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
199 // invariant-character conversion to UChars (see utypes.h and putil.h)
201 u_charsToUChars(currency
, uCurrency
, 4);
204 // in ICU 3.0 this API (which was @draft ICU 2.6) gained a UErrorCode& argument
205 #if (U_ICU_VERSION_MAJOR_NUM < 3)
206 nf
.setCurrency(uCurrency
);
208 nf
.setCurrency(uCurrency
, errorCode
);
212 static const char *const
214 // use locale IDs complete with country code to be sure to
215 // pick up number/currency format patterns
216 "en_US", "en_GB", "de_DE", "ja_JP", "fr_FR", "hi_IN"
219 static const char *const
221 "USD", "GBP", "EUR", "JPY"
225 showCurrencyFormatting(UBool useICU26API
) {
229 UnicodeString output
;
231 UErrorCode errorCode
;
233 // TODO: Using printf() here assumes that the runtime encoding is ASCII-friendly
234 // and can therefore be mixed with UTF-8
236 for(i
=0; i
<UPRV_LENGTHOF(sampleLocaleIDs
); ++i
) {
237 printf("show currency formatting (method for %s) in the locale \"%s\"\n",
238 useICU26API
? "ICU 2.6" : "before ICU 2.6",
241 // get a currency formatter for this locale ID
242 errorCode
=U_ZERO_ERROR
;
243 nf
=NumberFormat::createCurrencyInstance(sampleLocaleIDs
[i
], errorCode
);
244 if(U_FAILURE(errorCode
)) {
245 printf("NumberFormat::createCurrencyInstance(%s) failed - %s\n",
246 sampleLocaleIDs
[i
], u_errorName(errorCode
));
250 for(j
=0; j
<UPRV_LENGTHOF(sampleCurrencies
); ++j
) {
251 printf(" - format currency \"%s\": ", sampleCurrencies
[j
]);
253 // set the actual currency to be formatted
255 setNumberFormatCurrency_2_6(*nf
, sampleCurrencies
[j
], errorCode
);
257 setNumberFormatCurrency_2_4(*nf
, sampleCurrencies
[j
], errorCode
);
259 if(U_FAILURE(errorCode
)) {
260 printf("setNumberFormatCurrency(%s) failed - %s\n",
261 sampleCurrencies
[j
], u_errorName(errorCode
));
265 // output=formatted currency value
267 nf
->format(12345678.93, output
);
268 output
+=(UChar
)0x0a; // '\n'