Java floating-point formatting and java floating-point formatting
Overview
This section describes how to format floating point decimals Based on Java.
Body
In Java, the class used to format decimals is java. text. DecimalFormat. For example, you can use:
double data = 3334590479.22d;System.out.println(data); // 3.33459047922E9java.text.DecimalFormat df = new java.text.DecimalFormat("$#,###.##");String result = df.format(data);System.out.println(result); // $3,334,590,479.22
For relatively large numeric values, the mode uses scientific notation (scientific notation); If java. text. DecimalFormat is used to specify the format, the specified format is used.
The format "$ #, ######" contains the following semantics:
1. The prefix is $
2. Each 3-digit integer is a group.
3. retain up to 2 decimal places
About formats
The general structure of a format is:Prefixopt Number Suffixopt, That isPrefix+Number+SuffixThe prefix and suffix are optional.
About groups
Group Separator,It can only be used in the integer part. No matter how many delimiters are in the mode, the rightmost one is valid. The number of each group is the right integer digit of the rightmost separator.
So"#,##,###,####"="######,####"="##,####,####".
Decimal
Use decimal separator.To separate integer and decimal parts.
Special numeric characters
1.#
# It is a special character used in the digit section. It indicates that a number is displayed, but the first and last 0 are not displayed.
data:02222.20pattern:",###.##"
result:2,222.2
# There is no limit on the number of digits for the integer part, but it is used for the decimal part, but there is a "Maximum number of decimal places allowed", excessive rounding. For example:
data:2222.222pattern:",###.##"result:2,222.22
2.0
The usage of 0 and # is similar, but slightly different. The difference is:
A. 0 indicates the minimum number of digits in the integer part. If the number is not enough, add 0 to the front.
data:22.26
pattern:"0,000.00"result:0,022.26
B. 0 limit the number of digits in the decimal part. If the number is less than 0, multiple rounds are performed.
data:22.26
-----------------
pattern:"00.000"result:22.260
-----------------
pattern:"00.0"
result:22.3
3. E
Scientific notation, separator between the ending number and the index.
For more information, see the following document.
Prefix and suffix
Any Unicode character except \ uFFFE, \ uFFFF and special characters can be used as the prefix and suffix. There are four special characters:
1.%
Multiply the number by 100.
data:1234.567pattern:",###.##%"result:123,456.7%
2. \ u2030
Multiply the number by 1000.
data:1234.567pattern:",###.##\u2030"result:1,234,567‰
3. Seek (\ u00A4)
Localized currency symbols. If they appear twice in a row, they represent the international code of the currency symbol.
data:1234.567---------------------pattern:",###.##¤"result:1,234.57¥---------------------pattern:",###.##¤¤"result:1,234.57CNY
4 .'
Used to reference special characters as the prefix or suffix.
data:1234.567pattern:"'#',###.##"result:#1,234.57
If you want to use 'as the prefix or suffix, enter it twice in a row:
data:1234.567pattern:"'',###.##"result:'1,234.57
About scientific notation
Scientific notation is inNumberAdd content such as E0 at the end of the part, where E is the separator of the ending number and the index, and the number of 0 after E represents the minimum number of digits of the index (less than the first 0 ).
Data: 1E5pattern: 0. # E00result: 1E05 // note that the index has two digits
E must have at least one # or zero content before it. Otherwise, E will be treated as a prefix:
data:1E5---------------pattern:"E0"result:E100000---------------
pattern:"0E0"result:1E5---------------
pattern:"#E0"result:.1E6
The index part does not have much content. The ending part is discussed below.
The ending number generally includes integers and decimal places. If the integer part has no content (before "."), all valid numbers are in decimal places:
data:12345E5pattern:".###E0"result:.123E10
If the number of digits in the integer part needs to be retained, use a few zeros (the result of using # Is a bit incredible and you can try it on your own ):
data:12345E5----------------------pattern:"0.###E0"result:1.234E9----------------------pattern:"00.###E0"result:12.345E8---------------------pattern:"000.###E0"result:123.45E7
Decimal part. If you want to "retain a few decimal places at most", add the corresponding # After "." of "pattern #. In this way, the excess will be rounded off. If you want to achieve the effect of "retaining just a few decimal places", add the corresponding 0 after the. of pattern. In this way, the missing values are supplemented by 0, and the excess values are rounded.
data:123E5------------------pattern:"0.#E0"result:1.2E7------------------pattern:"0.##E0"result:1.23E7------------------pattern:"0.###E0"result:1.23E7==================pattern:"0.0E0"result:1.2E7------------------pattern:"0.00E0"result:1.23E7------------------pattern:"0.000E0"result:1.230E7