• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 /*
4 **********************************************************************
5 * Copyright (c) 2003-2010, International Business Machines
6 * Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 * Author: Mark Davis
9 * Created: May 22 2003
10 * Since: ICU 2.6
11 **********************************************************************
12 */
13 package com.ibm.icu.dev.demo.number;
14 import java.util.HashMap;
15 import java.util.Locale;
16 import java.util.Map;
17 
18 import com.ibm.icu.impl.Utility;
19 import com.ibm.icu.text.DecimalFormat;
20 import com.ibm.icu.text.DecimalFormatSymbols;
21 import com.ibm.icu.text.NumberFormat;
22 import com.ibm.icu.util.Currency;
23 
24 /**
25  * Demonstration code to illustrate how to obtain ICU 2.6-like currency
26  * behavior using pre-ICU 2.6 ICU4J.
27  * @author Mark Davis
28  */
29 public class CurrencyDemo {
30 
main(String[] args)31     public static void main(String[] args) {
32         testFormatHack(true);
33     }
34 
getCurrencyFormat(Currency currency, Locale displayLocale, boolean ICU26)35     static NumberFormat getCurrencyFormat(Currency currency,
36                                           Locale displayLocale,
37                                           boolean ICU26) {
38         // code for ICU 2.6
39         if (ICU26) {
40             NumberFormat result = NumberFormat.getCurrencyInstance(displayLocale);
41             result.setCurrency(currency);
42             return result;
43         }
44 
45         // ugly work-around for 2.4
46         DecimalFormat result = (DecimalFormat)NumberFormat.getCurrencyInstance(displayLocale);
47         HackCurrencyInfo hack = (HackCurrencyInfo)(hackData.get(currency.getCurrencyCode()));
48         result.setMinimumFractionDigits(hack.decimals);
49         result.setMaximumFractionDigits(hack.decimals);
50         result.setRoundingIncrement(hack.rounding);
51         DecimalFormatSymbols symbols = result.getDecimalFormatSymbols();
52         symbols.setCurrencySymbol(hack.symbol);
53         result.setDecimalFormatSymbols(symbols);
54         return result;
55     }
56 
57     static Map hackData = new HashMap();
58     static class HackCurrencyInfo {
59         int decimals;
60         double rounding;
61         String symbol;
HackCurrencyInfo(int decimals, double rounding, String symbol)62         HackCurrencyInfo(int decimals, double rounding, String symbol) {
63             this.decimals = decimals;
64             this.rounding = rounding;
65             this.symbol = symbol;
66         }
67     }
68     static {
69         hackData.put("USD", new HackCurrencyInfo(2, 0, "$"));
70         hackData.put("GBP", new HackCurrencyInfo(2, 0, "\u00A3"));
71         hackData.put("JPY", new HackCurrencyInfo(0, 0, "\u00A5"));
72         hackData.put("EUR", new HackCurrencyInfo(2, 0, "\u20AC"));
73     }
74 
75     /**
76      * Walk through all locales and compare the output of the ICU26
77      * currency format with the "hacked" currency format.
78      * @param quiet if true, only display discrepancies.  Otherwise,
79      * display all results.
80      */
testFormatHack(boolean quiet)81     static void testFormatHack(boolean quiet) {
82         String[] testCurrencies = {"USD","GBP","JPY","EUR"};
83         Locale[] testLocales = NumberFormat.getAvailableLocales();
84         for (int i = 0; i < testLocales.length; ++i) {
85             // since none of this should vary by country, we'll just do by language
86             if (!testLocales[i].getCountry().equals("")) continue;
87             boolean noOutput = true;
88             if (!quiet) {
89                 System.out.println(testLocales[i].getDisplayName());
90                 noOutput = false;
91             }
92             for (int j = 0; j < testCurrencies.length; ++j) {
93                 NumberFormat nf26 = getCurrencyFormat(Currency.getInstance(testCurrencies[j]), testLocales[i], true);
94                 String str26 = nf26.format(1234.567);
95                 if (!quiet) {
96                     System.out.print("\t" + Utility.escape(str26));
97                 }
98                 NumberFormat nf24 = getCurrencyFormat(Currency.getInstance(testCurrencies[j]), testLocales[i], false);
99                 String str24 = nf24.format(1234.567);
100                 if (!str24.equals(str26)) {
101                     if (noOutput) {
102                         System.out.println(testLocales[i].getDisplayName());
103                         noOutput = false;
104                     }
105                     if (quiet) {
106                         System.out.print("\t" + Utility.escape(str26));
107                     }
108                     System.out.print(" (" + Utility.escape(str24) + ")");
109                 }
110             }
111             if (!noOutput) {
112                 System.out.println();
113             }
114         }
115     }
116 }
117