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


Java Currency getNumericCodeAsString()用法及代碼示例

getNumericCodeAsString() 是 Java Currency 類的方法,用於獲取調用貨幣的 3 位數字代碼(由 ISO 4217 定義)作為字符串。例如,數值 20 將作為 "020" 返回,數值 1 將作為 "001" 返回。

用法

以下是 getNumericCodeAsString() 方法的聲明:

public String getNumericCodeAsString()

參數

數據類型 參數 描述
NA NA 此方法不接受任何參數。

返回

getNumericCodeAsString() 方法將調用貨幣的 3 位數字代碼(如 ISO 4217 定義)作為字符串返回。

異常

NA

兼容版本

Java 9 及以上

例子1

import java.util.*;
public class CurrencyGetNumericCodeAsStringExample1 {
   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.getNumericCodeAsString() +" - " +currency.getDisplayName());
	}
   }
}

輸出:

044 - Bahamian Dollar
064 - Bhutanese Ngultrum
788 - Tunisian Dinar
051 - Armenian Dram
442 - Luxembourgian Franc
963 - Testing Currency Code
000 - French Gold Franc
952 - West African CFA Franc
970 - Colombian Real Value Unit
380 - Italian Lira
760 - Syrian Pound
784 - United Arab Emirates Dirham
524 - Nepalese Rupee
108 - Burundian Franc
144 - Sri Lankan Rupee
124 - Canadian Dollar
752 - Swedish Krona
...

例子2

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

輸出:

Name:British Pound Sterling
Numeric Code:826

例子3

import java.util.*;
public class CurrencyGetNumericCodeAsStringExample3 {
   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:");
   	String numericCode = sc.next();
   	Set<Currency> currencySet = Currency.getAvailableCurrencies();
   	 for (Currency currency:currencySet) {
     		   if (currency.getNumericCodeAsString().equals(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:020
Currency with Numeric code 020 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 CurrencyGetNumericCodeAsStringExample4 {
   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.getNumericCodeAsString());
   }
}

輸出:

Enter the Currency:ADP
Name:Andorran Peseta
Numeric Code:020



相關用法


注:本文由純淨天空篩選整理自 Java Currency getNumericCodeAsString() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。