如何在Java中找到货币子单位(又名次要单位)符号?

2022-09-02 11:03:46

Currency.getSymbol会给我主要符号(例如,“$”代表美元),但我想得到次要单位(例如,“p”代表英镑或美分符号代表美元),而不用写我自己的查找表。

有没有一个标准,即以方式构建来做到这一点?


答案 1

我想建议在这种情况下使用 自定义 。使用或包装。有很多例子。custom currency formatDecimalFormatNumberFormatjava.text.*

public class CurrencyFormatExample {
    public void currencyFormat(Locale currentLocale) {
        Double currency = new Double(9843.21);
        NumberFormat currencyFormatter;
        String currencyOut;
        currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
        currencyOut = currencyFormatter.format(currency);
        System.out.println(currencyOut + " " + currentLocale.toString());
    }

    public static void main(String args[]) {
        Locale[] locales = new Locale[]{new Locale("fr", "FR"),
            new Locale("de", "DE"), new Locale("ca", "CA"),
            new Locale("rs", "RS"),new Locale("en", "IN")
        };
        CurrencyFormatExample[] formate = new CurrencyFormatExample[locales.length];
        for (int i = 0; i < locales.length; i++) {
            formate[i].currencyFormat(locales[i]);
        }
    }
}

出局:

9Â 843,21 â?¬ fr_FR

9.843,21 â?¬ de_DE

CAD 9.843,21 ca_CA

RSD 9,843.21 rs_RS

Rs.9,843.21 en_IN

参考这里

次要货币的更新

  Locale locale = Locale.UK;
  Currency curr = Currency.getInstance(locale);

  // get and print the symbol of the currency
  String symbol = curr.getSymbol(locale);
  System.out.println("Symbol is = " + symbol);

输出:

Symbol is = £

答案 2