我想获得印度的货币格式,所以我需要印度的Locale
对象。但是只存在少数Locale
常数(static final Locale
),而印度不是其中之一的国家。
要获取美国和英国的货币符号,我可以执行以下操作:
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());
}
使用常量Locale.US
和Locale.UK
。如果我想获得印度货币格式,我该怎么办?
答案 0 :(得分:26)
根据JDK release notes,您有区域代码hi_IN
(印地语)和en_IN
(英语)。
System.out.println(Currency.getInstance(new Locale("hi", "IN")).getSymbol());
答案 1 :(得分:18)
继承人你可以做的很简单,
float amount = 100000;
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
String moneyString = formatter.format(amount);
System.out.println(moneyString);
输出为,Rs.100,000.00。
答案 2 :(得分:1)
这是一个实用的方法来获得符号,无论你的语言环境是什么
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);
}
}
答案 3 :(得分:0)
请查看本指南:https://docs.oracle.com/javase/1.5.0/docs/guide/intl/locale.doc.html,印地语,印度有hi_IN
答案 4 :(得分: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
答案 5 :(得分:0)
我们必须手动为印度创建语言环境
Locale IND = new Locale("en", "IN");
NumberFormat india = NumberFormat.getCurrencyInstance(IND);
int money = 3456
System.out.print(india.format(money));
输出-Rs。 3,456