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