001: /*
002: **********************************************************************
003: * Copyright (c) 2003, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **********************************************************************
006: * Author: Mark Davis
007: * Created: May 22 2003
008: * Since: ICU 2.6
009: **********************************************************************
010: */
011: package com.ibm.icu.dev.demo.number;
012:
013: import com.ibm.icu.util.Currency;
014: import com.ibm.icu.text.NumberFormat;
015: import com.ibm.icu.text.DecimalFormat;
016: import com.ibm.icu.text.DecimalFormatSymbols;
017: import com.ibm.icu.impl.Utility;
018: import java.util.Locale;
019: import java.util.Map;
020: import java.util.HashMap;
021:
022: /**
023: * Demonstration code to illustrate how to obtain ICU 2.6-like currency
024: * behavior using pre-ICU 2.6 ICU4J.
025: * @author Mark Davis
026: */
027: public class CurrencyDemo {
028:
029: public static void main(String[] args) {
030: testFormatHack(true);
031: }
032:
033: static NumberFormat getCurrencyFormat(Currency currency,
034: Locale displayLocale, boolean ICU26) {
035: // code for ICU 2.6
036: if (ICU26) {
037: NumberFormat result = NumberFormat
038: .getCurrencyInstance(displayLocale);
039: result.setCurrency(currency);
040: return result;
041: }
042:
043: // ugly work-around for 2.4
044: DecimalFormat result = (DecimalFormat) NumberFormat
045: .getCurrencyInstance(displayLocale);
046: HackCurrencyInfo hack = (HackCurrencyInfo) (hackData
047: .get(currency.getCurrencyCode()));
048: result.setMinimumFractionDigits(hack.decimals);
049: result.setMaximumFractionDigits(hack.decimals);
050: result.setRoundingIncrement(hack.rounding);
051: DecimalFormatSymbols symbols = result.getDecimalFormatSymbols();
052: symbols.setCurrencySymbol(hack.symbol);
053: result.setDecimalFormatSymbols(symbols);
054: return result;
055: }
056:
057: static Map hackData = new HashMap();
058:
059: static class HackCurrencyInfo {
060: int decimals;
061: double rounding;
062: String symbol;
063:
064: HackCurrencyInfo(int decimals, double rounding, String symbol) {
065: this .decimals = decimals;
066: this .rounding = rounding;
067: this .symbol = symbol;
068: }
069: }
070:
071: static {
072: hackData.put("USD", new HackCurrencyInfo(2, 0, "$"));
073: hackData.put("GBP", new HackCurrencyInfo(2, 0, "\u00A3"));
074: hackData.put("JPY", new HackCurrencyInfo(0, 0, "\u00A5"));
075: hackData.put("EUR", new HackCurrencyInfo(2, 0, "\u20AC"));
076: }
077:
078: /**
079: * Walk through all locales and compare the output of the ICU26
080: * currency format with the "hacked" currency format.
081: * @param quiet if true, only display discrepancies. Otherwise,
082: * display all results.
083: */
084: static void testFormatHack(boolean quiet) {
085: String[] testCurrencies = { "USD", "GBP", "JPY", "EUR" };
086: Locale[] testLocales = NumberFormat.getAvailableLocales();
087: for (int i = 0; i < testLocales.length; ++i) {
088: // since none of this should vary by country, we'll just do by language
089: if (!testLocales[i].getCountry().equals(""))
090: continue;
091: boolean noOutput = true;
092: if (!quiet) {
093: System.out.println(testLocales[i].getDisplayName());
094: noOutput = false;
095: }
096: for (int j = 0; j < testCurrencies.length; ++j) {
097: NumberFormat nf26 = getCurrencyFormat(Currency
098: .getInstance(testCurrencies[j]),
099: testLocales[i], true);
100: String str26 = nf26.format(1234.567);
101: if (!quiet) {
102: System.out.print("\t" + Utility.escape(str26));
103: }
104: NumberFormat nf24 = getCurrencyFormat(Currency
105: .getInstance(testCurrencies[j]),
106: testLocales[i], false);
107: String str24 = nf24.format(1234.567);
108: if (!str24.equals(str26)) {
109: if (noOutput) {
110: System.out.println(testLocales[i]
111: .getDisplayName());
112: noOutput = false;
113: }
114: if (quiet) {
115: System.out.print("\t" + Utility.escape(str26));
116: }
117: System.out
118: .print(" (" + Utility.escape(str24) + ")");
119: }
120: }
121: if (!noOutput) {
122: System.out.println();
123: }
124: }
125: }
126: }
|