如何使用区域设置获取特定国家/地区的货币符号? [英] How to get the currency symbol for particular country using locale?

查看:153
本文介绍了如何使用区域设置获取特定国家/地区的货币符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过此代码,它为某些国家/地区代码货币符号

I have tried this code and it gives me the Country Codefor some countries instead of the currency symbol


我希望货币符号不是代码

数组resourseList包含所有带有代码的国家

String m= (String) Array.get(recourseList,i);
                    String[] ma=m.split(",");
                    Locale locale=new Locale("en", ma[1]);
                    Currency currency= Currency.getInstance(locale);
                    String symbol = currency.getSymbol();
                    ( (TextView) finalV.findViewById(R.id.currencySymbolid)).setText(symbol);


推荐答案

它在货币规范:

getSymbol()获取默认语言环境的货币符号。
例如,对于美元,如果默认的
区域设置是美国,则符号为$,而对于其他区域设置,它可能是US $。如果无法确定
符号,则返回ISO 4217货币代码。

getSymbol() gets the symbol of this currency for the default locale. For example, for the US Dollar, the symbol is "$" if the default locale is the US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code is returned.

已编辑
我找到了解决这个问题的方法

EDITED I have found a way around this issue

import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class CurrencyCode
{

    public static void main() {
        Map<Currency, Locale> map = getCurrencyLocaleMap();
        String [] countries = { "US", "CA", "MX", "GB", "DE", "PL", "RU", "JP", "CN" };

        for (String countryCode : countries) {
           Locale locale = new Locale("EN",countryCode);
           Currency currency = Currency.getInstance(locale);
           String symbol = currency.getSymbol(map.get(currency));
           System.out.println("For country " + countryCode + ", currency symbol is " + symbol);
        }
    }

    public static Map<Currency, Locale> getCurrencyLocaleMap() {
       Map<Currency, Locale> map = new HashMap<>();
        for (Locale locale : Locale.getAvailableLocales()) {
           try {
             Currency currency = Currency.getInstance(locale);
             map.put(currency, locale);
           }
           catch (Exception e){ 
             // skip strange locale 
           }
        }
        return map;
    }
}

打印:

For country US, currency symbol is $
For country CA, currency symbol is $
For country MX, currency symbol is $
For country GB, currency symbol is £
For country DE, currency symbol is €
For country PL, currency symbol is zł
For country RU, currency symbol is руб.
For country SE, currency symbol is kr
For country JP, currency symbol is ¥
For country CN, currency symbol is ¥

这篇关于如何使用区域设置获取特定国家/地区的货币符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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