tags:

views:

355

answers:

3

I want to get the currency format of INDIA. But there exists only few contries code. There is no country code for INDIA.

public void displayCurrencySymbols() {

    Currency currency = Currency.getInstance(Locale.US);
    System.out.println("United States: " + currency.getSymbol());

    currency = Currency.getInstance(Locale.UK);
    System.out.println("United Kingdom: " + currency.getSymbol());

}

But for US, UK are having the Locale. If i want to get INDIAN currency format, then what can i do far that? Is it possible to get all country currency format using java? Can you explain in a simple example?

+2  A: 

According to the JDK release notes, you have locale codes hi_IN (Hindi) and en_IN (English).

System.out.println(Currency.getInstance(new Locale("hi", "IN")).getSymbol());
Thilo
@Thilo: It works fine. Thanks a lot..
Venkats
A: 

look here http://java.sun.com/j2se/1.4.2/docs/guide/intl/locale.doc.html and there is Hindi India hi_IN

sanjuro
A: 

Here is an utility method to have the symbol, whatever is your locale

    public class Utils {

        public static SortedMap<Currency, Locale> currencyLocaleMap;

        static {
            currencyLocaleMap = new TreeMap<Currency, Locale>(new Comparator<Currency>() {
                @Override
                public int compare(Currency c1, Currency c2) {
                    return c1.getCurrencyCode().compareTo(c2.getCurrencyCode());
                }
            });

            for (Locale locale : Locale.getAvailableLocales()) {
                try {
                    Currency currency = Currency.getInstance(locale);
                    currencyLocaleMap.put(currency, locale);
                }
                catch (Exception e) {
                }
            }
        }


        public static String getCurrencySymbol(String currencyCode) {
            Currency currency = Currency.getInstance(currencyCode);
            return currency.getSymbol(currencyLocaleMap.get(currency));
        }

       public static String  getAmountAsFormattedString(Double amount, Double decimals, String currencyCode) {
            Currency currency = Currency.getInstance(currencyCode);
            double doubleBalance = 0.00;
            if (amount != null) {
                doubleBalance = ((Double) amount) / (Math.pow(10.0, decimals));
            }
            NumberFormat numberFormat = NumberFormat.getCurrencyInstance(currencyLocaleMap.get(currency));
            return numberFormat.format(doubleBalance);
    }


    }
cisco