How can I find out the currency sub-unit (aka minor unit) symbol in Java?

寵の児 提交于 2019-12-04 15:55:40

问题


Currency.getSymbol will give me the major symbol (e.g. "$" for USD) but I'd like to get the minor unit (e.g. "p" for GBP or the cents symbol for USD), without writing my own look up table.

Is there a standard, i.e. built in way to do this?


回答1:


I would like to suggest for this situation to use custom custom currency format. Use DecimalFormat or NumberFormat of java.text.* package. There are a lot of example for that.

Example

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]);
        }
    }
}

Out put:

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

Reference here:

Update for minor currency

  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);

Output :

Symbol is = £


来源:https://stackoverflow.com/questions/13341925/how-can-i-find-out-the-currency-sub-unit-aka-minor-unit-symbol-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!