/******************************************************************** * COPYRIGHT: * Copyright (c) 1999-2004, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ #include "unicode/utypes.h" #include "unicode/unistr.h" #include "unicode/numfmt.h" #include "unicode/dcfmtsym.h" #include "unicode/decimfmt.h" #include "unicode/locid.h" #include "unicode/uclean.h" #include "util.h" #include #include #include #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) extern "C" void capi(); void cppapi(); static void showCurrencyFormatting(UBool useICU26API); int main(int argc, char **argv) { printf("%s output is in UTF-8\n", argv[0]); printf("C++ API\n"); cppapi(); printf("C API\n"); capi(); showCurrencyFormatting(FALSE); showCurrencyFormatting(TRUE); u_cleanup(); // Release any additional storage held by ICU. printf("Exiting successfully\n"); return 0; } /** * Sample code for the C++ API to NumberFormat. */ void cppapi() { Locale us("en", "US"); UErrorCode status = U_ZERO_ERROR; // Create a number formatter for the US locale NumberFormat *fmt = NumberFormat::createInstance(us, status); check(status, "NumberFormat::createInstance"); // Parse a string. The string uses the digits '0' through '9' // and the decimal separator '.', standard in the US locale UnicodeString str("9876543210.123"); Formattable result; fmt->parse(str, result, status); check(status, "NumberFormat::parse"); printf("NumberFormat::parse(\""); // Display the result uprintf(str); printf("\") => "); uprintf(formattableToString(result)); printf("\n"); // Take the number parsed above, and use the formatter to // format it. str.remove(); // format() will APPEND to this string fmt->format(result, str, status); check(status, "NumberFormat::format"); printf("NumberFormat::format("); // Display the result uprintf(formattableToString(result)); printf(") => \""); uprintf(str); printf("\"\n"); delete fmt; // Release the storage used by the formatter } // currency formatting ----------------------------------------------------- *** /* * Set a currency on a NumberFormat with pre-ICU 2.6 APIs. * This is a "hack" that will not work properly for all cases because * only ICU 2.6 introduced a more complete framework and data for this. * * @param nf The NumberFormat on which to set the currency; takes effect on * currency-formatting NumberFormat instances. * This must actually be a DecimalFormat instance. * The display style of the output is controlled by nf (its pattern, * usually from the display locale ID used to create this instance) * while the currency symbol and number of decimals are set for * the currency. * @param currency The 3-letter ISO 4217 currency code, NUL-terminated. * @param errorCode ICU error code, must pass U_SUCCESS() on input. */ static void setNumberFormatCurrency_2_4(NumberFormat &nf, const char *currency, UErrorCode &errorCode) { // argument checking if(U_FAILURE(errorCode)) { return; } if(currency==NULL || strlen(currency)!=3) { errorCode=U_ILLEGAL_ARGUMENT_ERROR; return; } // check that the formatter is a DecimalFormat instance // necessary because we will cast to the DecimalFormat subclass to set // the currency symbol if(nf.getDynamicClassID()!=DecimalFormat::getStaticClassID()) { errorCode=U_ILLEGAL_ARGUMENT_ERROR; return; } // map the currency code to a locale ID // only the currencies in this array are supported // it would be possible to map to a locale ID, instantiate a currency // formatter for that and copy its values, but that would be slower, // and we have to hardcode something here anyway static const struct { // ISO currency ID const char *currency; // fractionDigits==minimumFractionDigits==maximumFractionDigits // for these currencies int32_t fractionDigits; /* * Set the rounding increment to 0 if it is implied with the number of * fraction digits. Setting an explicit rounding increment makes * number formatting slower. * In other words, set it to something other than 0 only for unusual * cases like "nickel rounding" (0.05) when the increment differs from * 10^(-maximumFractionDigits). */ double roundingIncrement; // Unicode string with the desired currency display symbol or name UChar symbol[16]; } currencyMap[]={ { "USD", 2, 0.0, { 0x24, 0 } }, { "GBP", 2, 0.0, { 0xa3, 0 } }, { "EUR", 2, 0.0, { 0x20ac, 0 } }, { "JPY", 0, 0.0, { 0xa5, 0 } } }; int32_t i; for(i=0; iformat(12345678.93, output); output+=(UChar)0x0a; // '\n' uprintf(output); } } }