Java (NumberFormat class), Java NumberFormat
1. NumberFormat indicates the formatting type of numbers. NumberFormat indicates the formatting type of numbers. That is, numbers can be displayed according to local style habits.
No. |
Method |
Type |
Description |
1 |
Public static Locale [] getAvailableLocales () |
Normal |
Returns an array of all language environments. |
2 |
Public static final NumberFormatGetInstance () |
Normal |
Returns the numeric format of the current default language environment. |
3 |
Public static NumberFormatGetInstance (Locale inLocale) |
Normal |
Returns the numeric format of the specified language environment. |
4 |
Public static final NumberFormatGetCurrencyInstance () |
Normal |
Returns the currency format of the current default environment. |
5 |
Public static NumberFormatGetCurrencyInstance (Locale inLocale) |
Normal |
Returns the numeric format of the specified language environment. |
Ii. Format numbers in the current language environment
Package com. pb. demo1; import java. text. numberFormat; public class NumberFormatTest {public static void main (String [] args) {NumberFormat nf = NumberFormat. getInstance (); System. out. println ("number displayed after formatting:" + nf. format (10000000); System. out. println ("number displayed after formatting:" + nf. format (10000.345 ));}}
Result:
Display number after formatting: 10,000,000 display number after formatting: 10,000.345
3. DecimalFormatDecimalFormat is also a subclass of Format. It is mainly used to Format numbers. Of course, it is more convenient to Format numbers than to directly use NumberFormat, because you can directly specify a custom formatting operation, similar to SimpleDateFormat, if you want to customize the formatting operation, you must specify a template for the formatting operation.
No. |
Mark |
Location |
Description |
1 |
0 |
Number |
Represents an Arabic number. Each 0 represents an Arabic number. If this digit does not exist, 0 is displayed. |
2 |
# |
Number |
Represents an Arabic number. Each digit # represents an Arabic number. If this digit does not exist, it is not displayed. |
3 |
. |
Number |
Decimal separator or decimal separator of currency |
4 |
- |
Number |
Negative number |
5 |
, |
Number |
Group Separator |
6 |
E |
Number |
Separate the ending number and index in scientific notation |
7 |
; |
Submode Boundary |
Separate Positive and Negative submodes |
8 |
% |
Prefix or suffix |
The number is multiplied by 100 and displayed as a percentage. |
9 |
\ U2030 |
Prefix or suffix |
Multiply by 1000 and display as a score |
10 |
Alibaba \ u00A4 |
Prefix or suffix |
Currency mark, which is replaced by the currency number. If both of them appear at the same time, replace them with the international currency symbol. If it appears in a certain mode, the decimal separator of the currency is used instead of the decimal separator. |
11 |
, |
Prefix or suffix |
It is used to enclose special characters in the prefix or suffix with quotation marks. For example, "'#" format 123 to "#123 ". To create a single quotation mark, use two single quotation marks consecutively: "# o '''clock ". |
Package com. pb. demo1; import java. text. decimalFormat; public class FormatDemo {public void format (String pattern, double value) {DecimalFormat df = new DecimalFormat (pattern); String str = df. format (value); System. out. println ("use" + pattern + "\ t format number" + value + ": \ t" + str);} public static void main (String [] args) {FormatDemo demo = new FormatDemo (); demo. format ("###,###. ### ", 111222.34567); demo. format ("000,000.000", 11222.34567); demo. format ("###,###. ### $ ", 111222.34567); demo. format ("000,000.000 $", 11222.34567); demo. format ("##. ### % ", 0.345678); // demo in percent format. format ("00. ### % ", 0.0345678); // demo in percent format. format ("###. ### \ u2030 ", 0.345678); // use the thousands of scores }}