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

7

52

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?

Klapp answered 30/3, 2010 at 10:34 Comment(5)
@Thilo: How can I implement it?Klapp
@Venkats : put this comment under his answer, or he won't see it.Alvie
Note: There are more locales in the JDK than the JRE.Microgamete
@Thorbjørn Ravn Andersen: that is weird. Why did they do that?Grimm
@Thilo, my guess would be download size...Microgamete
G
60

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());
Grimm answered 30/3, 2010 at 10:41 Comment(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().Kochi
L
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 .

Logographic answered 30/8, 2013 at 10:57 Comment(2)
It works fine. But I prefer to have rupee symbol instead of Rs. Any quick workaround?Coble
With out symbol RS. is there any solution to get this value like 100,000.00Fifi
C
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

Cartulary answered 12/6, 2020 at 15:27 Comment(0)
E
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);
    }


    }
Emerald answered 8/6, 2010 at 13:5 Comment(0)
T
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

Tiernan answered 30/3, 2010 at 10:44 Comment(0)
C
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
Chug answered 1/8, 2019 at 11:7 Comment(1)
This might answer to the question, please try to give some details with a solution to the problem.Trotskyism
G
0

In java 8 you get the "Rs", but in java 15 you get the rupia symbol. I don't know how to get the "Rs" in java 15.

Give answered 1/2 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.