0

I wrote a java function that shows the locale pattern for each currency. See the function below. What I am interested to know is that why when the currency is CHF, the 2nd decimal is hardcoded to 5?

Here is the output of the function which is related to USD and CHF currencies:

Analyzing currency: [USD] localePattern: [¤#,##0.00;(¤#,##0.00)] Currency symbol [$]
Analyzing currency: [CHF] localePattern: [¤#,##0.05;(¤#,##0.05)] Currency symbol [SwF]

Here is the java function I wrote:

import com.ibm.icu.text.DecimalFormat; 
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.util.Currency;

public static void main(String[] args)
  {
    Currency configuredCurrency                       = null; 
    NumberFormat formatter                = NumberFormat.getCurrencyInstance();
    DecimalFormat localeCurrencyFormatter = (DecimalFormat)formatter;
    String localePattern                  = "";
    String symbol                         = "";

    String currencies        = "AED,AFN,ALL,AMD,ARS,AUD,BGN,BHD,BND,BOB,BRL,BWP,BYR,CAD,CHF,CLP,CNY,COP,CRC,CZK,DJF,DKK,DOP,DZD,EEK,EGP,ERN,ETB,EUR,GBP,GTQ,HKD,HNL,HRK,HUF,IDR,ILS,INR,IQD,IRR,ISK,JOD,JPY,KES,KPW,KRW,KWD,KZT,LBP,LTL,LVL,LYD,MAD,MKD,MTL,MXN,MYR,NIO,NOK,NZD,OMR,PAB,PEN,PHP,PKR,PLN,PYG,QAR,RON,RUB,SAR,SDD,SEK,SGD,SKK,SOS,SVC,SYP,SwF,THB,TND,TRY,TZS,UAH,USD,UYU,VEB,VND,YER,ZAR,ZWD";
    String[] currenciesArray = currencies.split(",");

    for (int i = 0; i < currenciesArray.length; i++)
    {
      String currency = currenciesArray[i];
      configuredCurrency = Currency.getInstance(currency);
      localeCurrencyFormatter.setCurrency(configuredCurrency);
      localePattern = localeCurrencyFormatter.toPattern();
      symbol = localeCurrencyFormatter.getCurrency().getSymbol();

      System.out.println("Analyzing currency: [" + currency + "] localePattern: [" + localePattern + "] Currency symbol [" + symbol + "]");
    } 
  }
Steven R. Loomis
  • 4,228
  • 28
  • 39
Wael
  • 1,533
  • 4
  • 20
  • 35
  • Which locale you are running from? – Jayamohan Jan 08 '13 at 10:20
  • the default en_US locale – Wael Jan 08 '13 at 10:24
  • Am getting the output properly for English. ...Analyzing currency: [CHF] localePattern: [¤#,##0.00] Currency symbol [CHF]... – Jayamohan Jan 08 '13 at 10:27
  • Me too, and later there is an exception for "SwF". What Java version are you using? – lbalazscs Jan 08 '13 at 10:36
  • 2
    The same behaviour for me. Exception with "SwF" and correct output with CHF. This is probably a bug in an old Java version. Because the smallest unit in CHF is 0.05 which is also the smallest coin available. But in some electronic applications, such as adding interest payments etc. this rule is broken and smaller units than CHF 0.05 are being used and this is probably the reason this was changed. – rob Jan 08 '13 at 11:08
  • have to check if CHF has/hasn't second decimal possition in current JVM, by default is currency based on real paper, metal cash, real CHF cash haven't got second decimal place – mKorbel Jan 08 '13 at 11:09
  • @Jayamohan I forgot to mention that in my project, we are not using these imports: import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Currency; But instead we are using these: import com.ibm.icu.text.DecimalFormat; import com.ibm.icu.text.NumberFormat; import com.ibm.icu.util.Currency; – Wael Jan 08 '13 at 12:01
  • I just fixed the imports in my question above. Now it should replicate with anyone – Wael Jan 08 '13 at 12:06
  • I have retagged the question to include the icu tag. You could ask the members of the icu project directly – lbalazscs Jan 08 '13 at 12:41
  • flagged as duplicate of http://stackoverflow.com/questions/14220750/changing-the-pattern-for-chf-currency-in-java-using-icu-package – Steven R. Loomis Jan 15 '13 at 17:53

0 Answers0