Java的NumberFormat getCompactNumberInstance() 方法和示例
NumberFormat 是所有数字格式的抽象基类。此类提供格式化和解析数字的接口。 NumberFormat 类还提供了用于确定哪些语言环境(美国、意大利等)具有数字格式及其名称的方法
NumberFormat 帮助您格式化和解析任何语言环境的数字。您的代码可以完全独立于小数点、千位分隔符、甚至所使用的特定十进制数字的区域设置约定,或者数字格式是否为偶数格式。
类 NumberFormat 层次结构:
java.lang.Object
java.text.Format
java.text.NumberFormat
构造函数:Constructor Description NumberFormat() Sole constructor. (For invocation by subclass constructors, typically implicit.)
语法:
public abstract class NumberFormat extends Format
getCompactNumberInstance() 方法:
注意:getCompactNumberInstance() 方法是在Java 12 中添加的。所以请确保您的机器上安装了Java 12 或更高版本。您可以通过以下方式检查Java版本:
java -version
在cmd中输入上述命令并检查版本。它应该在Java 12 以上。
输出
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
注意:此方法是在Java 12 中添加的。所以请确保您的机器上安装了Java 12 或更高版本。可以通过polling命令查看Java版本如下:
java -version
在cmd中输入上述命令并检查版本。它应该在Java 12 以上。
输出:
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
Here the Java version is 13.0.2.
执行:
方法一: getCompactNumberInstance()
它是Java.text.NumberFormat 的一个内置方法,它返回默认 FORMAT 语言环境的紧凑数字格式,具有“SHORT”格式样式。
句法:
public static NumberFormat getCompactNumberInstance()
参数:该函数不接受任何参数。
返回值:用于压缩数字格式的 NumberFormat 实例。
例子
Java
// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance() Method
// Importing required classes
import java.util.*;
import java.text.NumberFormat;
import java.util.Locale;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an object of NumberFormat class
NumberFormat nf
= NumberFormat.getCompactNumberInstance();
// Print and display commands
System.out.println(
"This method returns in NumberFormat.Style.SHORT:");
System.out.println("Result: " + nf.format(100));
System.out.println("Result: " + nf.format(10000));
System.out.println("Result: " + nf.format(120300));
System.out.println("Result: " + nf.format(2120000));
System.out.println("Result: "
+ nf.format(1240000300));
}
}
Java
// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance() Method
// Importing required classes
import java.text.NumberFormat;
import java.util.Locale;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of NumberFormat class
NumberFormat nfLong
= NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.LONG);
// Print and display commands
System.out.println(nfLong.format(100));
System.out.println(nfLong.format(1000));
System.out.println(nfLong.format(10000));
System.out.println(nfLong.format(100000));
// Again creating an object of NumberFormat class
NumberFormat nfShort
= NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.SHORT);
// Print and display commands
System.out.println(nfShort.format(100));
System.out.println(nfShort.format(1000));
System.out.println(nfShort.format(10000));
System.out.println(nfShort.format(100000));
}
}
Java
// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance() Method
// Importing required classes
import java.text.NumberFormat;
import java.util.Locale;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Creating an object of NumberFormat class
NumberFormat fmt
= NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.LONG);
// Print and display commands
System.out.println(fmt.parse("100"));
System.out.println(fmt.parse("1 thousand"));
System.out.println(fmt.parse("10 thousand"));
System.out.println(fmt.parse("100 thousand"));
}
}
输出:
This method returns in NumberFormat.Style.SHORT:
Result: 100
Result: 10K
Result: 120K
Result: 2M
Result: 1B
方法二: getCompactNumberInstance()
它 是Java.text.NumberFormat 的内置方法,它返回指定语言环境和 formatStyle 的紧凑数字格式。
语法:
public static NumberFormat getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle)
参数:该函数接受 2个参数:
- 所需的语言环境
- 格式化数字的样式
返回值:用于压缩数字格式的 NumberFormat 实例
异常:如果 locale 或 formatStyle 为 null,则此方法抛出NullPointerException
示例 1
Java
// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance() Method
// Importing required classes
import java.text.NumberFormat;
import java.util.Locale;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of NumberFormat class
NumberFormat nfLong
= NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.LONG);
// Print and display commands
System.out.println(nfLong.format(100));
System.out.println(nfLong.format(1000));
System.out.println(nfLong.format(10000));
System.out.println(nfLong.format(100000));
// Again creating an object of NumberFormat class
NumberFormat nfShort
= NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.SHORT);
// Print and display commands
System.out.println(nfShort.format(100));
System.out.println(nfShort.format(1000));
System.out.println(nfShort.format(10000));
System.out.println(nfShort.format(100000));
}
}
输出:
100
1 thousand
10 thousand
100 thousand
100
1K
10K
100K
示例 2
Java
// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance() Method
// Importing required classes
import java.text.NumberFormat;
import java.util.Locale;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Creating an object of NumberFormat class
NumberFormat fmt
= NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.LONG);
// Print and display commands
System.out.println(fmt.parse("100"));
System.out.println(fmt.parse("1 thousand"));
System.out.println(fmt.parse("10 thousand"));
System.out.println(fmt.parse("100 thousand"));
}
}
输出:
100
1000
10000
100000
Note: In the above program, we have used fmt.parse() method to parse compact numbers into LONG pattern.