Venkat
Venkat

Reputation: 21480

Get the currency format for a country that does not have a Locale constant

I want to get the currency format of India, so I need a Locale object for India. But there exists only few a countries that have a Locale constant (a static final Locale), and India is not one of them.

To get the currency symbols for the US and UK, I can do the following:

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());

}

That uses the constants Locale.US and Locale.UK. If i want to get the Indian currency format, what can I do?

Upvotes: 52

Views: 66328

Answers (6)

Anupam Haldkar
Anupam Haldkar

Reputation: 1085

We have to manually create locale for India

Locale IND = new Locale("en", "IN");
NumberFormat india = NumberFormat.getCurrencyInstance(IND);
int money = 3456
System.out.print(india.format(money));

Output - Rs.3,456

Upvotes: 5

a_di
a_di

Reputation: 1

import java.text.NumberFormat;
import java.util.Locale;

double dbValue = 1_23_23_213.89;
Locale lcl = new Locale("hi","IN");
NumberFormat inFrmt = NumberFormat.getNumberInstance(lcl);
System.out.println(inFrmt.format(dbValue));      //12,323,213.89

NumberFormat cur = NumberFormat.getCurrencyInstance(lcl);
System.out.println(cur.format(dbValue));      // ₹12,323,213.89

Upvotes: 0

sanjuro
sanjuro

Reputation: 1661

Look at this guide: https://docs.oracle.com/javase/1.5.0/docs/guide/intl/locale.doc.html and there is hi_IN for Hindi, India

Upvotes: 0

Thilo
Thilo

Reputation: 262494

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());

Upvotes: 61

CleanX
CleanX

Reputation: 1176

heres is simple thing u can do ,

  float amount = 100000;

  NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));

  String moneyString = formatter.format(amount);

  System.out.println(moneyString);

The output will be , Rs.100,000.00 .

Upvotes: 36

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);
    }


    }

Upvotes: 1

Related Questions