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


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


getNumericCode() 是 Java Currency 类的方法,用于获取调用货币的数字代码(由 ISO 4217 定义)。

用法

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

public int getNumericCode()

参数

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

返回

getNumericCode() 方法返回调用货币的数字代码(由 ISO 4217 定义)。

异常

NA

兼容版本

Java 1.7 及以上

例子1

import java.util.*;
public class CurrencyGetNumericCodeExample1 {
   public static void main(String args[]) {
    // Get all available currencies
		Set<Currency> setCurrency = Currency.getAvailableCurrencies();
		// Print Currency Code of all the currency
		for (Currency currency:setCurrency) {
			  System.out.println(currency.getNumericCode() +" - " +currency.getDisplayName());
		}
   }
}

输出:

230 - Ethiopian Birr
188 - Costa Rican Colรณn
132 - Cape Verdean Escudo
934 - Turkmenistani Manat
20 - Andorran Peseta
292 - Gibraltar Pound
970 - COU
975 - Bulgarian Lev
932 - Zimbabwean Dollar (2009)
344 - Hong Kong Dollar
214 - Dominican Peso
634 - Qatari Rial
...

例子2

import java.util.Currency;
import java.util.Locale;
public class CurrencyGetNumericCodeExample2 {
   public static void main(String args[]) {
      Locale locale = Locale.UK;
      Currency cur = Currency.getInstance(locale);

      System.out.println("Name:" +cur.getDisplayName());
      System.out.println("Numeric code:" +cur.getNumericCode());
   }
}

输出:

Name:British Pound Sterling
Numeric Code:826

例子3

import java.util.*;
public class CurrencyGetNumericCodeExample3 {
   public static void main(String args[]) {
   	int flag = 0;
   	Scanner sc = new Scanner(System.in);
   	System.out.print("\n Enter the Numeric code which you want to search:");
   	int numericCode = sc.nextInt();
   	Set<Currency> currencySet = Currency.getAvailableCurrencies();
   	 for (Currency currency:currencySet) {
     		   if (currency.getNumericCode() == numericCode) {
        			flag = 1;
        			break;
      		  }
	}
	if (flag == 1){
		System.out.println("\n Currency with numeric code "  + numericCode + " is found. \n");
	}
	else
		System.out.println("\n Currency with numeric code "  + numericCode + " not found. \n");		
	}
}

输出:

Enter the numeric code which you want to search:356
Currency with Numeric code 356 is found.

Enter the numeric code which you want to search:111
Currency with Numeric code 111 not found.

示例 4

import java.util.Currency;
import java.util.Locale;
import java.util.Scanner;
public class CurrencyGetNumericCodeExample4 {
   public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("\n Enter the Currency:");
        String name = sc.next();
        Currency cur = Currency.getInstance(name);
        System.out.println("\n Name:" + cur.getDisplayName());
        System.out.println("\n Numeric code:" + cur.getNumericCode());
   }
}

输出:

Enter the Currency:INR
Name:Indian Rupee
Numeric Code:356



相关用法


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