📜  Java中的 Currency getDefaultFractionDigits() 方法及示例

📅  最后修改于: 2022-05-13 01:54:54.893000             🧑  作者: Mango

Java中的 Currency getDefaultFractionDigits() 方法及示例

Java中Currency 类getDefaultFractionDigits()方法用于检索或了解该货币的小数位数,这是 ISO 4217 货币代码设置的默认值。

句法:

public int getDefaultFractionDigits()

参数:此方法不接受任何参数。

返回值:此方法返回一个整数值,它是货币的默认小数位数。

异常:如果调用无效代码,该方法将引发运行时错误

下面的程序说明了 getDefaultFractionDigits() 方法的工作:

方案一:

// 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

方案二:

// 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