当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Currency getDisplayName()用法及代码示例


有两种类型的 getDisplayName 方法。

Java 货币 getDisplayName() 方法

getDisplayName() 是Java Currency 类的方法,用于获取调用默认语言环境的货币名称。

用法

以下是 getDisplayName() 方法的声明:

public String getDisplayName()

参数

数据类型 参数 描述
NA NA 此方法不接受任何参数。

返回

getDisplayName() 方法用于获取调用默认语言环境的货币名称。

异常

NA

兼容版本

Java 1.7 及以上

例子1

import java.util.Currency;
public class CurrencyGetDisplayNameExample1 {
	public static void main(String[] args) {	
		// create an instance of Currency class
		Currency currency = Currency.getInstance("INR");
		// prints the value for currency INR
		System.out.println("INR is:"+currency.getDisplayName());		
	}
}

输出:

INR is:Indian Rupee

例子2

import java.util.Currency;
import java.util.Locale; 
public class CurrencyGetDisplayNameExample2 {
    public static void main(String[] args) {
        Currency currency = Currency.getInstance(Locale.US);
        System.out.println("Currency:" + currency);
        System.out.println("Display name:"+ currency.getDisplayName());

        currency = Currency.getInstance(Locale.CHINA);
        System.out.println("Currency:" + currency);
        System.out.println("Display name:"+ currency.getDisplayName());
    }
}

输出:

Currency:USD
Display name:US Dollar
Currency:CNY
Display name:Chinese Yuan

例子3

import java.util.*;
public class CurrencyGetDisplayNameExample3 {
   public static void main(String args[]) {
      // Create a currency for GERMANY locale
      Locale locale = Locale.GERMANY;
      Currency loc = Currency.getInstance(locale);
      System.out.println("Name of given Currency is:"+loc.getDisplayName());
    }
}

输出:

Name of given Currency is:Euro

示例 4

import java.util.Currency;
import java.util.Scanner; 
public class CurrencyGetDisplayNameExample4 {
    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());
    }
}

输出:

Enter the Currency:INR
Currency name is:Indian Rupee

Java Currency getDisplayName(Locale locale) 方法

getDisplayName(Locale locale) 是Java Currency 类的方法,用于获取调用指定语言环境的货币名称。

用法

以下是 getDisplayName(Locale locale) 方法的声明:

public String getDisplayName(Locale locale)

参数

数据类型 参数 描述
NA locale locale 是需要给定货币的显示名称。

返回

getDisplayName(Locale locale) 方法返回调用指定语言环境的货币名称。

异常

NullPointerException - 如果语言环境为 Null。

兼容版本

Java 1.7 及更高版本。

例子1

import java.util.Currency;
import java.util.Locale;
public class CurrencyGetDisplayNameLocaleExample1 {
   public static void main(String args[]) {
      Locale locale = Locale.US;
      Currency cur = Currency.getInstance(locale);
      //Prints the name of the given locale's currency
      System.out.println(cur.getDisplayName(Locale.US));
   }
}

输出:

US Dollar

例子2

import java.util.Currency;
import java.util.Locale;
public class CurrencyGetDisplayNameLocaleExample2 {
    public static void main(String[] args) {
        Currency currency = Currency.getInstance(Locale.CHINA);
        System.out.println("Currency:" + currency);
        System.out.println("French display name:"+ currency.getDisplayName(Locale.FRANCE));
        System.out.println("Korean display name:"+ currency.getDisplayName(Locale.KOREA));
    }
}

输出:

Currency:CNY
French display name:yuan renminbi chinois
Korean display name:중국 위안 인민폐

例子3

import java.util.Currency;
import java.util.Locale;
public class CurrencyGetDisplayNameLocaleExample3 {
    public static void main(String[] args) {
    	Locale locale = new Locale("en"); //en for English
		Currency curr = Currency.getInstance("INR");
		System.out.println(curr.getDisplayName(locale));
		 locale = new Locale("es"); //es for Spain
		 curr = Currency.getInstance("INR");
		System.out.println(curr.getDisplayName(locale));
		locale = new Locale("ru"); //ru for Russia
		 curr = Currency.getInstance("INR");
		System.out.println(curr.getDisplayName(locale));
	}
}

输出:

Indian Rupee
rupia india 
Indian Rupee

示例 4

import java.util.Currency;
import java.util.Scanner;
import java.util.Locale; 
public class CurrencyGetDisplayNameLocaleExample4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the Locale:");  
        String l = sc.next();
        System.out.print("Enter the Currency:");  
        String c = sc.next();
        Locale locale = new Locale(l);
		Currency curr = Currency.getInstance(c);
		System.out.println("Default locale is:"+ curr.getDisplayName());
		System.out.println("Specified locale is:"+curr.getDisplayName(locale));
    }
}

输出:

Enter the Locale:IT
Enter the Currency:INR
Default locale is:Indian Rupee
Specified locale is:Rupia Indiana

Enter the locale:EN
Enter the Currency:USD
Default locale is:US Dollar
Specified locale is:US Dollar



相关用法


注:本文由纯净天空筛选整理自 Java Currency getDisplayName() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。