我如何使用" R"当语言环境设置为南非时的货币?
目前它显示" ZAR"我需要它来展示" R"代替。
我试过了:
final Currency curr = Currency.getInstance("ZAR");
final NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.getDefault());
nf.setCurrency(curr);
final String value = nf.format(12345.67);
然后一旦完成所有这些,我用R using.replace();
替换了ZAR答案 0 :(得分:2)
NumberFormat
类根据作为参数传递的区域设置来格式化数字。如果该区域设置适用于南非(new Locale("en", "ZA")
),则该数字将正确显示:R 12,345.67
final Currency curr = Currency.getInstance("ZAR");
final NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("en", "ZA"));
nf.setCurrency(curr);
final String value = nf.format(12345.67);
System.out.println(value);
答案 1 :(得分:0)
构建自己的NumberFormat
类(扩展Java),并实现自己的format
版本;为ZAR
以外的所有货币调用基类函数。
您目前采用的方法往往会导致大量重复(和未封装的)代码。
答案 2 :(得分:0)
好的,我发现了一个很好的例子:
public static SortedMap<Currency, Locale> currencyLocaleMap;
static {
currencyLocaleMap = new TreeMap<Currency, Locale>(new Comparator<Currency>() {
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);
System.out.println( currencyCode+ ":-" + currency.getSymbol(currencyLocaleMap.get(currency)));
return currency.getSymbol(currencyLocaleMap.get(currency));
}