OLD | NEW |
1 /******************************************************************** | 1 /******************************************************************** |
2 * COPYRIGHT: | 2 * COPYRIGHT: |
3 * Copyright (c) 1999-2010, International Business Machines Corporation and | 3 * Copyright (c) 1999-2014, International Business Machines Corporation and |
4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
5 ********************************************************************/ | 5 ********************************************************************/ |
6 | 6 |
7 #include "unicode/utypes.h" | 7 #include "unicode/utypes.h" |
8 #include "unicode/unistr.h" | 8 #include "unicode/unistr.h" |
9 #include "unicode/numfmt.h" | 9 #include "unicode/numfmt.h" |
10 #include "unicode/dcfmtsym.h" | 10 #include "unicode/dcfmtsym.h" |
11 #include "unicode/decimfmt.h" | 11 #include "unicode/decimfmt.h" |
12 #include "unicode/locid.h" | 12 #include "unicode/locid.h" |
13 #include "unicode/uclean.h" | 13 #include "unicode/uclean.h" |
14 #include "util.h" | 14 #include "util.h" |
15 #include <stdio.h> | 15 #include <stdio.h> |
16 #include <stdlib.h> | 16 #include <stdlib.h> |
17 #include <string.h> | 17 #include <string.h> |
18 | 18 |
19 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) | |
20 | |
21 extern "C" void capi(); | 19 extern "C" void capi(); |
22 void cppapi(); | 20 void cppapi(); |
23 | 21 |
24 static void | 22 static void |
25 showCurrencyFormatting(UBool useICU26API); | 23 showCurrencyFormatting(UBool useICU26API); |
26 | 24 |
27 int main(int argc, char **argv) { | 25 int main(int argc, char **argv) { |
28 printf("%s output is in UTF-8\n", argv[0]); | 26 printf("%s output is in UTF-8\n", argv[0]); |
29 | 27 |
30 printf("C++ API\n"); | 28 printf("C++ API\n"); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 UChar symbol[16]; | 144 UChar symbol[16]; |
147 } currencyMap[]={ | 145 } currencyMap[]={ |
148 { "USD", 2, 0.0, { 0x24, 0 } }, | 146 { "USD", 2, 0.0, { 0x24, 0 } }, |
149 { "GBP", 2, 0.0, { 0xa3, 0 } }, | 147 { "GBP", 2, 0.0, { 0xa3, 0 } }, |
150 { "EUR", 2, 0.0, { 0x20ac, 0 } }, | 148 { "EUR", 2, 0.0, { 0x20ac, 0 } }, |
151 { "JPY", 0, 0.0, { 0xa5, 0 } } | 149 { "JPY", 0, 0.0, { 0xa5, 0 } } |
152 }; | 150 }; |
153 | 151 |
154 int32_t i; | 152 int32_t i; |
155 | 153 |
156 for(i=0; i<LENGTHOF(currencyMap); ++i) { | 154 for(i=0; i<UPRV_LENGTHOF(currencyMap); ++i) { |
157 if(strcmp(currency, currencyMap[i].currency)==0) { | 155 if(strcmp(currency, currencyMap[i].currency)==0) { |
158 break; | 156 break; |
159 } | 157 } |
160 } | 158 } |
161 if(i==LENGTHOF(currencyMap)) { | 159 if(i==UPRV_LENGTHOF(currencyMap)) { |
162 // a more specific error code would be useful in a real application | 160 // a more specific error code would be useful in a real application |
163 errorCode=U_UNSUPPORTED_ERROR; | 161 errorCode=U_UNSUPPORTED_ERROR; |
164 return; | 162 return; |
165 } | 163 } |
166 | 164 |
167 // set the currency-related data into the caller's formatter | 165 // set the currency-related data into the caller's formatter |
168 | 166 |
169 nf.setMinimumFractionDigits(currencyMap[i].fractionDigits); | 167 nf.setMinimumFractionDigits(currencyMap[i].fractionDigits); |
170 nf.setMaximumFractionDigits(currencyMap[i].fractionDigits); | 168 nf.setMaximumFractionDigits(currencyMap[i].fractionDigits); |
171 | 169 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 NumberFormat *nf; | 226 NumberFormat *nf; |
229 int32_t i, j; | 227 int32_t i, j; |
230 | 228 |
231 UnicodeString output; | 229 UnicodeString output; |
232 | 230 |
233 UErrorCode errorCode; | 231 UErrorCode errorCode; |
234 | 232 |
235 // TODO: Using printf() here assumes that the runtime encoding is ASCII-frie
ndly | 233 // TODO: Using printf() here assumes that the runtime encoding is ASCII-frie
ndly |
236 // and can therefore be mixed with UTF-8 | 234 // and can therefore be mixed with UTF-8 |
237 | 235 |
238 for(i=0; i<LENGTHOF(sampleLocaleIDs); ++i) { | 236 for(i=0; i<UPRV_LENGTHOF(sampleLocaleIDs); ++i) { |
239 printf("show currency formatting (method for %s) in the locale \"%s\"\n"
, | 237 printf("show currency formatting (method for %s) in the locale \"%s\"\n"
, |
240 useICU26API ? "ICU 2.6" : "before ICU 2.6", | 238 useICU26API ? "ICU 2.6" : "before ICU 2.6", |
241 sampleLocaleIDs[i]); | 239 sampleLocaleIDs[i]); |
242 | 240 |
243 // get a currency formatter for this locale ID | 241 // get a currency formatter for this locale ID |
244 errorCode=U_ZERO_ERROR; | 242 errorCode=U_ZERO_ERROR; |
245 nf=NumberFormat::createCurrencyInstance(sampleLocaleIDs[i], errorCode); | 243 nf=NumberFormat::createCurrencyInstance(sampleLocaleIDs[i], errorCode); |
246 if(U_FAILURE(errorCode)) { | 244 if(U_FAILURE(errorCode)) { |
247 printf("NumberFormat::createCurrencyInstance(%s) failed - %s\n", | 245 printf("NumberFormat::createCurrencyInstance(%s) failed - %s\n", |
248 sampleLocaleIDs[i], u_errorName(errorCode)); | 246 sampleLocaleIDs[i], u_errorName(errorCode)); |
249 continue; | 247 continue; |
250 } | 248 } |
251 | 249 |
252 for(j=0; j<LENGTHOF(sampleCurrencies); ++j) { | 250 for(j=0; j<UPRV_LENGTHOF(sampleCurrencies); ++j) { |
253 printf(" - format currency \"%s\": ", sampleCurrencies[j]); | 251 printf(" - format currency \"%s\": ", sampleCurrencies[j]); |
254 | 252 |
255 // set the actual currency to be formatted | 253 // set the actual currency to be formatted |
256 if(useICU26API) { | 254 if(useICU26API) { |
257 setNumberFormatCurrency_2_6(*nf, sampleCurrencies[j], errorCode)
; | 255 setNumberFormatCurrency_2_6(*nf, sampleCurrencies[j], errorCode)
; |
258 } else { | 256 } else { |
259 setNumberFormatCurrency_2_4(*nf, sampleCurrencies[j], errorCode)
; | 257 setNumberFormatCurrency_2_4(*nf, sampleCurrencies[j], errorCode)
; |
260 } | 258 } |
261 if(U_FAILURE(errorCode)) { | 259 if(U_FAILURE(errorCode)) { |
262 printf("setNumberFormatCurrency(%s) failed - %s\n", | 260 printf("setNumberFormatCurrency(%s) failed - %s\n", |
263 sampleCurrencies[j], u_errorName(errorCode)); | 261 sampleCurrencies[j], u_errorName(errorCode)); |
264 continue; | 262 continue; |
265 } | 263 } |
266 | 264 |
267 // output=formatted currency value | 265 // output=formatted currency value |
268 output.remove(); | 266 output.remove(); |
269 nf->format(12345678.93, output); | 267 nf->format(12345678.93, output); |
270 output+=(UChar)0x0a; // '\n' | 268 output+=(UChar)0x0a; // '\n' |
271 uprintf(output); | 269 uprintf(output); |
272 } | 270 } |
273 } | 271 } |
274 } | 272 } |
OLD | NEW |