如何将国家代码转换为对应的货币?

发布于 2024-11-17 12:35:30 字数 238 浏览 3 评论 0原文

我需要根据用户在我的应用程序中将国家/地区代码转换为货币。

根据我的地理位置库,我知道用户是否位于美国(US)、德国(DE)、瑞士(CH)、日本(JPY)等。现在我需要从国家代码转换为相应的货币:

US -> USD ($)
DE -> EURO (€)
CH -> CHF (CHF)
JP -> YEN (JPY)

我怎样才能在Java中做到这一点?

I need to convert a country code to a currency in my app on a user basis.

Based on my geolocation library, I know whether the user is in the United States (US), Germany (DE), Switzerland (CH), Japan (JPY), etc. Now I need to convert from the country code to the corresponding currency:

US -> USD ($)
DE -> EURO (€)
CH -> CHF (CHF)
JP -> YEN (JPY)

How can I do this in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-11-24 12:35:30

是的。标准 JDK 支持主要外币:

System.out.println(DecimalFormat.getCurrencyInstance(Locale.US).format(12.34));
System.out.println(DecimalFormat.getCurrencyInstance(Locale.FRANCE).format(12.34));

输出:

$12.34
12,34 €

希望这有帮助。

Yes. The standard JDK supports major foreign currencies:

System.out.println(DecimalFormat.getCurrencyInstance(Locale.US).format(12.34));
System.out.println(DecimalFormat.getCurrencyInstance(Locale.FRANCE).format(12.34));

Outputs:

$12.34
12,34 €

Hope this is helpful.

╭⌒浅淡时光〆 2024-11-24 12:35:30

您应该解析此页面并将其存储在您的数据库中。

从互联网来源构建适合应用程序需求的良好数据库很困难,但这确实值得。要解析 html,我建议您尝试正则表达式,如果它变得太复杂,请使用 apache jericho标签汤

You should parse this page and store it in your database.

It's hard to build good databases suited to an apps need from Internet sources, but it's really worth it. To parse html, I suggest you try regexp, and if it gets too complicated, please use apache jericho or tag soup.

丑丑阿 2024-11-24 12:35:30

这又如何呢?这是一个黑客,但似乎工作正常。

/**
 * 
 */
package lt;

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

/**
 * LocaleTest - LocaleTest
 * @version 1.0
 * @author <a href="mailto:[email protected]">Chris Dennett</a>
 */
public class LocaleTest {
    public static final Map<String, Currency> CURRENCIES = new HashMap<String, Currency>();
    static {
        for (Locale l : Locale.getAvailableLocales()) {
            try {
                Currency c = Currency.getInstance(l);

                if (c != null) {
                    CURRENCIES.put(l.getCountry(), c);
                    System.out.println("key: " + l.getCountry() + ", loc: " + l + ", curr: " + c + " (" + c.getSymbol(l) + ")");                
                }
            } catch (IllegalArgumentException e) {

            }
        }
    }

    public static final void main(String args[]) {
        if (args == null || args.length == 0 || (args[0] = args[0].trim()).isEmpty()) {
            System.out.println("Please provide an argument!");
            return;
        }
        System.out.println("LocaleTest: got " + args[0] + " as argument");
        Currency c = CURRENCIES.get(args[0]);
        if (c != null) {
            System.out.println("LocaleTest: got " + c + " (" + c.getSymbol() + ") as currency");
        } else {
            System.out.println("LocaleTest: no currency found for " + args[0]);
        }
    }
}

给定 GB 的输出:

...
key: YE, loc: ar_YE, curr: YER (?.?.?)
key: MK, loc: mk_MK, curr: MKD (Den)
key: CA, loc: en_CA, curr: CAD ($)
key: VN, loc: vi_VN, curr: VND (?)
key: NL, loc: nl_NL, curr: EUR (€)
key: US, loc: es_US, curr: USD (US$)
key: CN, loc: zh_CN, curr: CNY (?)
key: HN, loc: es_HN, curr: HNL (L)
key: US, loc: en_US, curr: USD ($)
..
LocaleTest: got GB as argument
LocaleTest: got GBP as currency

问题是货币可能会将名称显示为符号而不是符号($ £ 等)。货币必须在其原产国的区域设置下创建才能发挥作用。如果修复此问题,您的货币可能仍然带有看起来不正确的符号。

What about this? It's a hack but seems to work okay.

/**
 * 
 */
package lt;

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

/**
 * LocaleTest - LocaleTest
 * @version 1.0
 * @author <a href="mailto:[email protected]">Chris Dennett</a>
 */
public class LocaleTest {
    public static final Map<String, Currency> CURRENCIES = new HashMap<String, Currency>();
    static {
        for (Locale l : Locale.getAvailableLocales()) {
            try {
                Currency c = Currency.getInstance(l);

                if (c != null) {
                    CURRENCIES.put(l.getCountry(), c);
                    System.out.println("key: " + l.getCountry() + ", loc: " + l + ", curr: " + c + " (" + c.getSymbol(l) + ")");                
                }
            } catch (IllegalArgumentException e) {

            }
        }
    }

    public static final void main(String args[]) {
        if (args == null || args.length == 0 || (args[0] = args[0].trim()).isEmpty()) {
            System.out.println("Please provide an argument!");
            return;
        }
        System.out.println("LocaleTest: got " + args[0] + " as argument");
        Currency c = CURRENCIES.get(args[0]);
        if (c != null) {
            System.out.println("LocaleTest: got " + c + " (" + c.getSymbol() + ") as currency");
        } else {
            System.out.println("LocaleTest: no currency found for " + args[0]);
        }
    }
}

Output with GB given:

...
key: YE, loc: ar_YE, curr: YER (?.?.?)
key: MK, loc: mk_MK, curr: MKD (Den)
key: CA, loc: en_CA, curr: CAD ($)
key: VN, loc: vi_VN, curr: VND (?)
key: NL, loc: nl_NL, curr: EUR (€)
key: US, loc: es_US, curr: USD (US$)
key: CN, loc: zh_CN, curr: CNY (?)
key: HN, loc: es_HN, curr: HNL (L)
key: US, loc: en_US, curr: USD ($)
..
LocaleTest: got GB as argument
LocaleTest: got GBP as currency

An issue is that a currency may display the name as symbol rather than the symbol ($ £ etc.). The currency has to be created with the locale of its originating country for that to work. If you fix this, you might still have currencies with symbols that don't look right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文