如何找到Java中的货币子单位(又称小单位)符号? [英] How can I find out the currency sub-unit (aka minor unit) symbol in Java?

查看:79
本文介绍了如何找到Java中的货币子单位(又称小单位)符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Currency.getSymbol将给我主要符号(例如,美元为 $),但我想获得次要单位(例如,GB为 p或美元为美分),而无需编写自己的外观桌子。

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?

推荐答案

对于这种情况,我建议使用自定义自定义货币格式。使用 java.text。* 包的 DecimalFormat NumberFormat
有很多示例。

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.

示例

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 = £

这篇关于如何找到Java中的货币子单位(又称小单位)符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆