Java NumberFormat getPercentInstance()方法及示例
- getPercentInstance()方法是java.text.NumberFormat的一个内置方法,用于返回当前默认FORMAT地区的百分比格式。
语法 :
public static final NumberFormat getPercentInstance()
参数:该函数不接受任何参数。
返回值:该函数返回用于百分比格式化的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 percent instance NumberFormat nF = NumberFormat.getPercentInstance(); // 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 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 percent instance NumberFormat nF = NumberFormat .getPercentInstance(); // 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#getPercentInstance()
- getPercentInstance(Locale inLocale)方法是java.text.NumberFormat的一个内置方法,用于返回任何指定区域的百分比格式。
语法 :
public static NumberFormat getPercentInstance(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 percent instance NumberFormat nF = NumberFormat .getPercentInstance( 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#getPercentInstance(java.util.Locale)
