Not getting currency symbols for specific Locale(没有获取特定语言环境的货币符号)
问题描述
我正在尝试根据语言环境获取货币符号.但不是返回符号,而是返回代码.我有一个片段:
I am trying to get the symbols of the currencies based on their Locale. But instead of returning a symbol, it is returning the code. I have a snippet:
import java.util.Currency;
import java.util.Locale;
public class CurrencyFormat
{
public void displayCurrencySymbols()
{
Currency currency = Currency.getInstance(Locale.US);
System.out.println("United States: " + currency.getSymbol());
}
public static void main(String[] args)
{
new CurrencyFormat().displayCurrencySymbols();
}
}
对于 Locale.US 它给出符号 $
但如果我替换
For Locale.US it is giving symbol $
but If I replace
Currency currency = Currency.getInstance(Locale.US);
与
Currency currency = Currency.getInstance(Locale.GERMANY);
然后它不是符号,而是给出国家代码.为什么会这样以及我们如何获得符号?
Then instead of symbol it is giving the country code. Why is this and how we can get the symbols?
在查看了一些答案后,我想明确设置一些特定的默认本地不是解决方案,因为我需要一次显示所有可用的标志.
EDIT : After looking some answer I would like to clear that setting some specific default local is not a solution as I need all the avalaible sign displayed at once.
例如
Locale.setDefault(Locale.UK);
会给我欧元符号,但对于doller,它将给出代码而不是doller符号($).
will give me the euro sign but for doller it will give the code instead of doller sign($).
推荐答案
你好,请尝试以下代码
import java.text.NumberFormat;
import java.util.Comparator;
import java.util.Currency;
import java.util.Locale;
import java.util.SortedMap;
import java.util.TreeMap;
public class CurrencyExample
{
public static void main(String[] args)
{
Utils.getCurrencySymbol( Currency.getInstance(Locale.US).getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.JAPAN).getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.UK).getCurrencyCode());
Utils.getCurrencySymbol("INR");
}
}
class Utils{
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));
}
}
上面程序的输出是这样的:
The output of above program is like that:
USD:-$
JPY:-¥
GBP:-£
INR:-Rs.
这篇关于没有获取特定语言环境的货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有获取特定语言环境的货币符号
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01