使用" R"而不是" ZAR"对于南非货币

时间:2016-02-11 10:43:52

标签: java android currency-formatting

我如何使用" 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

3 个答案:

答案 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));
  }

从这里开始:Not getting currency symbols for specific Locale