Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

currency

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?

like image 328
Venkat Avatar asked Mar 30 '10 10:03

Venkat


People also ask

How can I get currency from locale?

The getSymbol() method is used to get the symbol of a given currency for the default DISPLAY locale. For example, for the US Dollar, the symbol is "$" if the default locale is the US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code is returned.

How do I get Rs in Java?

format(amount); System. out. println(moneyString); The output will be Rs.

What is Java locale?

The Java Locale class object represents a specific geographic, cultural, or political region. It is a mechanism to for identifying objects, not a container for the objects themselves. A Locale object logically consists of the fields like languages, script, country, variant, extensions.


3 Answers

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());
like image 128
Thilo Avatar answered Oct 17 '22 03:10

Thilo


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 .

like image 36
CleanX Avatar answered Oct 17 '22 01:10

CleanX


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

like image 4
Anupam Haldkar Avatar answered Oct 17 '22 01:10

Anupam Haldkar