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


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