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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。