Java NumberFormat getIntegerInstance()方法及示例

来源:这里教程网 时间:2026-02-17 20:59:28 作者:

Java NumberFormat getIntegerInstance()方法及示例

    getIntegerInstance()方法是java.text.NumberFormat的一个内置方法,用于返回当前默认FORMAT区域的整数格式。

语法 :

public static final NumberFormat getIntegerInstance()

参数 :该函数不接受任何参数。

返回值 :该函数返回NumberFormat实例,用于对整数值进行通用格式化。

下面是上述函数的实现。

程序 1:

// Java program to implement// the above functionimport java.text.NumberFormat;import java.util.Locale;import java.util.Currency;public class Main {    public static void main(String[] args) throws Exception    {          // Get the integer instance        NumberFormat nF = NumberFormat.getIntegerInstance();          // Sets the currency to Canadian Dollar        nF.setCurrency(Currency.getInstance(Locale.CANADA));          // Stores the values        String values = nF.getCurrency().getDisplayName();          // Prints the currency        System.out.println(values);    }}

输出:

Canadian Dollar

程序 2:

// Java program to implement// the above function  import java.text.NumberFormat;import java.util.Locale;import java.util.Currency;  public class Main {    public static void main(String[] args)        throws Exception    {          // Get the integer instance        NumberFormat nF            = NumberFormat                  .getIntegerInstance();          // Stores the values        String values            = nF.getCurrency()                  .getDisplayName();          // Prints the currency        System.out.println(values);    }}

输出:

US Dollar

参考: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#getIntegerInstance()

    getIntegerIntegerInstance(Locale inLocale)方法是java.text.NumberFormat的一个内置方法,它为任何指定的locale返回一个整数的数字格式。

语法 :

public static NumberFormat getIntegerInstance(Locale inLocale)

参数 : 该函数接受一个强制性参数inLocale,描述要指定的语言。

返回值 : 该函数返回NumberFormat实例,用于对整数值进行格式化。

下面是上述函数的实现。

程序 1:

// Java program to implement// the above function  import java.text.NumberFormat;import java.util.Locale;import java.util.Currency;  public class Main {    public static void main(String[] args)        throws Exception    {          // Get the integer instance        NumberFormat nF            = NumberFormat                  .getIntegerInstance(                      Locale.CANADA);          // Stores the values        String values            = nF.getCurrency()                  .getDisplayName();          // Prints the currency        System.out.println(values);    }}

输出:

Canadian Dollar

参考: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#getIntegerInstance(java.util.Locale)

相关推荐