java 货币符号_java使用Currency类获得指定国家的货币符号

2015-08-19 06:30:01

阅读( 389 )

下面示例通过Currency类的getInstance()方法获得指定国家的货币信息,然后通过Currency.getSymbol()获得其货币符号。

package cn.outofmemory.examples;

import java.util.Currency;

import java.util.Locale;

/**

*

* @author outofmemory.cn

*/

public class Main {

public void displayCurrencySymbols() {

Currency currency = Currency.getInstance(Locale.US);

System.out.println("United States: " + currency.getSymbol());

currency = Currency.getInstance(Locale.UK);

System.out.println("United Kingdom: " + currency.getSymbol());

currency = Currency.getInstance(Locale.FRANCE);

System.out.println("France: " + currency.getSymbol());

}

public static void main(String[] args) {

new Main().displayCurrencySymbols();

}

}

上述代码将输出:

United States: USD

United Kingdom: GBP

France: €

分享给朋友:

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息:

面试题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Java程序,它可以将两种货币之间的汇率进行转换。在程序中,我们使用了一个HashMap来存储货币之间的汇率。您可以根据需要对其进行修改。 ```java import java.util.HashMap; import java.util.Scanner; public class CurrencyConverter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); HashMap<String, Double> exchangeRates = new HashMap<>(); exchangeRates.put("USD", 1.0); exchangeRates.put("EUR", 0.85); exchangeRates.put("GBP", 0.75); exchangeRates.put("JPY", 111.28); exchangeRates.put("CNY", 6.47); System.out.print("Enter the amount: "); double amount = scanner.nextDouble(); System.out.print("Enter the currency to convert from: "); String fromCurrency = scanner.next().toUpperCase(); System.out.print("Enter the currency to convert to: "); String toCurrency = scanner.next().toUpperCase(); double fromRate = exchangeRates.get(fromCurrency); double toRate = exchangeRates.get(toCurrency); double result = amount * (toRate / fromRate); System.out.println(amount + " " + fromCurrency + " is equivalent to " + result + " " + toCurrency + "."); } } ``` 运行程序后,您可以输入要转换的金额、要转换的货币和要转换成的货币,程序会计算并输出转换后的金额。例如,如果您要将100美元转换为欧元,您可以输入以下内容: ``` Enter the amount: 100 Enter the currency to convert from: USD Enter the currency to convert to: EUR ``` 程序将输出: ``` 100.0 USD is equivalent to 85.0 EUR. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值