-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberFormatFeatureExample.java
More file actions
30 lines (21 loc) · 1.26 KB
/
Copy pathNumberFormatFeatureExample.java
File metadata and controls
30 lines (21 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Java12;
import java.text.NumberFormat;
import java.util.Locale;
// NumberFormat.getCompactNumberInstance() method returns the instance of NumberFormat for compact number formatting.
//use case: to format a number in a compact form like 1K, 1M, 1B, etc.
//NumberFormat.getCompactNumberInstance() method returns the instance of NumberFormat for compact number formatting.
//NumberFormat.getCompactNumberInstance(Locale locale, NumberFormat.Style style) method returns the instance of NumberFormat for compact number formatting with the specified locale and style.
//The style can be SHORT or LONG.
public class NumberFormatFeatureExample {
public static String getCompactNumberFormat(int i) {
return NumberFormat.getCompactNumberInstance().format(i);
}
public static String getCompactNumberFormat(int i, Locale locale, NumberFormat.Style style) {
return NumberFormat.getCompactNumberInstance(locale, style).format(i);
}
public static void main(String[] args) {
System.out.println(getCompactNumberFormat(1000));//1K
System.out.println(getCompactNumberFormat(1000, Locale.US, NumberFormat.Style.SHORT));//1K
System.out.println(getCompactNumberFormat(1000, Locale.US, NumberFormat.Style.LONG));//1 thousand
}
}