有两种类型的 getIsntance 方法。
Java 货币 getSymbol() 方法
getSymbol() 是 Java Currency 类的方法,用于获取默认 DISPLAY 语言环境的给定货币的符号。例如,美元的符号是 "$"。
用法
以下是 getSymbol() 方法的声明:
public String getSymbol()
参数
数据类型 | 参数 | 描述 |
---|---|---|
NA | NA | 此方法不接受任何参数。 |
返回
getSymbol() 方法用于获取调用默认语言环境的货币符号。
异常
NA
兼容版本
Java 1.4 及以上
例子1
import java.util.Currency;
public class CurrencyGetSymbolExample1 {
public static void main(String args[]) {
// Create a currency for USD
Currency cur = Currency.getInstance("USD");
// Get and print the symbol of the currency
String symbol = cur.getSymbol();
System.out.println("Currency symbol is:" + symbol);
}
}
输出:
Currency symbol is:$
例子2
import java.util.Locale;
import java.util.Currency;
public class CurrencyGetSymbolExample2 {
public static void main(String[] args) {
Currency us = Currency.getInstance(Locale.US);
System.out.println("Currency symbol of US:" + us.getSymbol());
Currency germany = Currency.getInstance(Locale.GERMANY);
System.out.println("Currency symbol of Germany:" + germany.getSymbol());
Currency uk = Currency.getInstance(Locale.UK);
System.out.println("Currency symbol of UK:" + uk.getSymbol());
}
}
输出:
Currency Symbol of US:$ Currency symbol of Germany:EUR Currency symbol of UK:GBP
例子3
import java.util.*;
public class CurrencyGetSymbolExample3 {
public static void main(String[] args) {
Currency cur = Currency.getInstance("EUR");
//In default locale
System.out.println("Currency symbol of EUR is:" + cur.getSymbol());
Locale.setDefault(Locale.UK);
//Change the default locale
System.out.println("Currency symbol of EUR is:" + cur.getSymbol());
}
}
输出:
Currency symbol of EUR is:EUR Currency symbol of EUR is:?
示例 4
import java.util.Currency;
import java.util.Scanner;
public class CurrencyGetSymbolExample4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the currency:");
String name=sc.next();
Currency currency = Currency.getInstance(name);
System.out.println("Currency name is:"+ currency.getDisplayName());
System.out.println("Currency Symbol is:"+ currency.getSymbol());
}
}
输出:
Enter the Currency:EUR Currency name is:Euro Currency Symbol is:EUR Enter the Currency:USD Currency name is:US Dollar Currency Symbol is:$
Java Currency getSymbol(Locale locale) 方法
getSymbol(Locale locale) 是Java Currency 类的方法,用于获取调用指定locale 的货币符号。例如,对于美元,如果指定本地为US,则符号为"$",否则可能为"US$"。
用法
以下是 getSymbol(Locale locale) 方法的声明:
public String getSymbol(Locale locale)
参数
数据类型 | 参数 | 描述 |
---|---|---|
NA | locale | locale 是需要给定货币的显示名称。 |
返回
getSymbol(Locale locale) 方法返回指定语言环境的调用货币符号。
异常
NullPointerException ?如果语言环境为 Null。
兼容版本
Java 1.4 及以上
例子1
import java.util.*;
public class CurrencyGetSymbolLocaleExample1 {
public static void main(String args[]) {
// create a currency for uk locale
Locale locale = Locale.UK;
Currency curr = Currency.getInstance(locale);
// get and print the symbol of the currency
System.out.println("Currency symbol is:" + curr.getSymbol(locale));
}
}
输出:
Currency symbol is:ÂŁ
例子2
import java.util.*;
public class CurrencyGetSymbolLocaleExample2 {
public static void main(String[] args) {
Currency us = Currency.getInstance(Locale.US);
System.out.println("Currency Symbol:- " + us.getSymbol());
System.out.println("Currency Symbol With Locale:- " + us.getSymbol(Locale.US));
Currency uk = Currency.getInstance(Locale.UK);
System.out.println("\nCurrency Symbol:- " + uk.getSymbol());
System.out.println("Currency Symbol With Locale:- " + uk.getSymbol(Locale.UK));
Currency germany = Currency.getInstance(Locale.GERMANY);
System.out.println("\nCurrency Symbol:- " + germany.getSymbol());
System.out.println("Currency Symbol With Locale:- " + germany.getSymbol(Locale.GERMANY));
}
}
输出:
Currency Symbol:$ Currency Symbol with Locale:$ Currency Symbol:GBP Currency Symbol with Locale:ÂŁ Currency Symbol:EUR Currency Symbol with Locale:?
例子3
import java.util.Currency;
import java.util.Locale;
public class CurrencyGetSymbolLocaleExample3{
public void displayCurrencySymbols() {
Currency currency = Currency.getInstance(Locale.US);
System.out.println("United States:" + currency.getSymbol(Locale.US));
}
public static void main(String[] args){
new CurrencyGetSymbolLocaleExample3().displayCurrencySymbols();
}
}
输出:
United States:$
示例 4
import java.util.Currency;
import java.util.Locale;
public class CurrencyGetSymbolLocaleExample4 {
public static void main(String args[]) {
Currency currency = Currency.getInstance("USD");
System.out.println("USD:"+currency.getSymbol());
System.out.println("USD:"+currency.getSymbol(Locale.CANADA));
System.out.println("USD:"+currency.getSymbol(new Locale("en","AU")));
}
}
输出:
USD:$ USD:US$ USD:USD
相关用法
- Java Currency getSymbol()用法及代码示例
- Java Currency getSymbol用法及代码示例
- Java Currency getDisplayName()用法及代码示例
- Java Currency getInstance()用法及代码示例
- Java Currency getDefaultFractionDigits()用法及代码示例
- Java Currency getCurrencyCode()用法及代码示例
- Java Currency getNumericCode()用法及代码示例
- Java Currency getAvailableCurrencies()用法及代码示例
- Java Currency getNumericCodeAsString()用法及代码示例
- Java Currency toString()用法及代码示例
- Java ChronoPeriod isZero()用法及代码示例
- Java Class getDeclaredMethod()用法及代码示例
- Java Character isLetter()用法及代码示例
- Java Class getComponentType()用法及代码示例
- Java ConcurrentLinkedDeque add()用法及代码示例
- Java ChronoZonedDateTime from()用法及代码示例
- Java Class getSuperClass()用法及代码示例
- Java ChronoZonedDateTime hashCode()用法及代码示例
- Java ChronoLocalDate getChronology()用法及代码示例
- Java ConcurrentSkipListSet iterator()用法及代码示例
注:本文由纯净天空筛选整理自 Java Currency getSymbol() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。