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


Java Currency getSymbol用法及代碼示例


Java中的Currency類的getSymbol()方法用於檢索貨幣代碼的正式符號。

用法:

CURRENCY.getSymbol()

參數:此方法不接受任何參數。


返回值:此方法返回貨幣的官方符號。

異常:如果調用了無效的代碼,該方法將引發運行時錯誤。

以下示例程序旨在說明getSymbol()方法的用法:

示例1:

// Java Code to illustrate getSymbol() method 
  
import java.util.*; 
  
public class Currency_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating a currency with the code 
        Currency curr_ency 
            = Currency.getInstance("INR"); 
  
        // Getting the symbol of the currency 
        String currency_symbol 
            = curr_ency.getSymbol(); 
        System.out.println("Symbol for the currency of India is: "
                           + currency_symbol); 
    } 
}
輸出:
Symbol for the currency of India is: INR

示例2:

// Java Code to illustrate getSymbol() method 
  
import java.util.*; 
  
public class Currency_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating a currency with the code 
        Currency curr_ency 
            = Currency.getInstance("USD"); 
  
        // Getting the symbol of the currency 
        String currency_symbol 
            = curr_ency.getSymbol(); 
        System.out.println("Symbol for the currency of USA is: "
                           + currency_symbol); 
    } 
}
輸出:
Symbol for the currency of USA is: $

示例3:輸入無效的貨幣代碼。

// Java Code to illustrate getSymbol() method 
  
import java.util.*; 
  
public class Currency_Demo { 
    public static void main(String[] args) 
    { 
        try { 
  
            // Creating a currency with the code 
            Currency curr_ency 
                = Currency.getInstance("USDA"); 
  
            // Getting the symbol of the currency 
            String currency_symbol 
                = curr_ency.getSymbol(); 
            System.out.println("Symbol for the currency of USA is: "
                               + currency_symbol); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
java.lang.IllegalArgumentException


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Currency getSymbol Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。