Java:基于 ISO 4217 货币代码的货币符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3994016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:17:34  来源:igfitidea点击:

Java: Currency symbol based on ISO 4217 currency cod

java

提问by Eternal Learner

Below program prints the Currency symbol given the ISO 4217 currency code.

下面的程序打印给定 ISO 4217 货币代码的货币符号。

import java.util.*;

public class Currency{

    public static void main(String args[]) {
        Currency myInstance = Currency.getInstance(args[0]);
        System.out.println(myInstance.getSymbol());
    }
}

Problem: Works fine when the string USD is input. For other inputs like EUR just return the Currency Code.

问题:输入字符串 USD 时工作正常。对于 EUR 等其他输入,只需返回货币代码。

Sample input , ouput from the program:

示例输入,来自程序的输出:

input: java Currency USD 
output: $
input: java Currency EUR
output: EUR -> I expect the symbol of Euro here

回答by polygenelubricants

Currency.getSymbol()returns the currency symbol relative to the default locale.

Currency.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.

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

Use Currency.getSymbol(Locale locale)if you want the symbol for a different locale.

Currency.getSymbol(Locale locale)如果您想要不同语言环境的符号,请使用。

System.out.println(
   Currency.getInstance("USD").getSymbol(Locale.US)
);
// prints $

System.out.println(
   Currency.getInstance("USD").getSymbol(Locale.FRANCE)
);
// prints USD

System.out.println(
   Currency.getInstance("EUR").getSymbol(Locale.US)
);
// prints EUR

System.out.println(
   Currency.getInstance("EUR").getSymbol(Locale.FRANCE)
);
// prints 

(see also on ideone.com).

另见 ideone.com)。

回答by Alex Abdugafarov

For me, your code even in the first case returns USD. It seemes, that Currency heavily depends on the JRE version (1.6 for me). Perosnally I recommend you to write your own CUR to symbol conversion module - it will be much easier, than try to use this one.

对我来说,即使在第一种情况下,您的代码也会返回美元。看来,货币在很大程度上取决于 JRE 版本(对我来说是 1.6)。Perosnally 我建议您编写自己的 CUR 到符号转换模块 - 比尝试使用这个模块要容易得多。

回答by teh.fonsi

If someone needs it the other way round (for example -> EUR)

如果有人反过来需要它(例如 -> EUR)

String currency = ;
String currencyCode = "";
for (Currency c : Currency.getAvailableCurrencies()) {
    if (c.getSymbol().equals(currency)) {
        currencyCode = c.toString();
    }
}

回答by s.k

Using the limited Locale enum's only caters for westernised symbols. If you want to be a little more global try using Locales provided by:

使用有限的 Locale 枚举仅适用于西方化的符号。如果您想更加全球化,请尝试使用以下提供的 Locales:

Locale[] locales = Locale.getAvailableLocales();

Using Locales from this list gave symbols rather than TLA's pretty consistently.

使用此列表中的 Locales 可以非常一致地提供符号而不是 TLA。