當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。