50

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?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Venkat
  • 20,802
  • 26
  • 75
  • 84

6 Answers6

59

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());
exploitr
  • 843
  • 1
  • 14
  • 27
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    The key thing is that 1) The `Locale` class is not a collection of constant values; it also has constructors. 2) All it is is an identifier for a locale; the real work is done in `Currency.getInstance()`. – Raedwald Aug 30 '13 at 12:30
36

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 .

CleanX
  • 1,158
  • 3
  • 17
  • 25
5

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

Anupam Haldkar
  • 985
  • 13
  • 15
1

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


    }
0

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

exploitr
  • 843
  • 1
  • 14
  • 27
sanjuro
  • 1,611
  • 1
  • 21
  • 29
0
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
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
a_di
  • 1
  • 2