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


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


Java中的getDefaultFractionDigits()貨幣類方法用於檢索或知道該貨幣的小數位數,這是ISO 4217貨幣代碼設置的默認值。

用法:

public int getDefaultFractionDigits()

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


返回值:此方法返回整數值,該整數值是貨幣的默認小數位數。

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

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

示例1:

// Java Code to illustrate 
// getDefaultFractionDigits() 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 fraction digit of the currency 
        int currency_fracdig 
            = curr_ency.getDefaultFractionDigits(); 
        System.out.println("INR's fractions digits are: "
                           + currency_fracdig); 
    } 
}
輸出:
INR's fractions digits are: 2

示例2:

// Java Code to illustrate 
// getDefaultFractionDigits() 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("JOD"); 
  
        // Getting the fraction digit of the currency 
        int currency_fracdig 
            = curr_ency.getDefaultFractionDigits(); 
        System.out.println("Jordan's fractions digits are: "
                           + currency_fracdig); 
    } 
}
輸出:
Jordan's fractions digits are: 3

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

// Java Code to illustrate 
// getDefaultFractionDigits() 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("JODA"); 
  
            // Getting the fraction digit of the currency 
            int currency_fracdig 
                = curr_ency.getDefaultFractionDigits(); 
            System.out.println("Jordan's fractions digits are: "
                               + currency_fracdig); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
java.lang.IllegalArgumentException


相關用法


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