Formatting of Java floating-point types

Source: Internet
Author: User

Overview

Based on Java, describes a scheme for formatting decimals in floating-point types.

Body

In Java, the class used to format decimals is Java.text.DecimalFormat, such as you can use:

double data = 3334590479.22d//  3.33459047922E9New Java.text.DecimalFormat ("$#,###.##"=//  $3,334,590,479.22

For larger values, the pattern uses the scientific notation (scientific notation), and the format is specified using Java.text.DecimalFormat.

The format "$#,###.##" contains the following semantics:

1. The prefix is $
2. Integer part every 3 digits is a group of
3. Retain up to 2 decimal places

About formatting

The approximate structure of a format is:prefixopt number suffixopt, which is prefix + digit + suffix Where both the prefix and the suffix are optional.

About grouping

The group delimiter , is used and can only be used in integer parts; Regardless of the number of separators in the pattern, the rightmost one is valid; The number of each group is the right integer number of the rightmost delimiter.

So "#,##,###,####" = = "######,####" = "##,####,####" .

About decimals

The decimal separator . is used to separate the integer and fractional parts.

About Numeric special characters

1. #
#是一个特殊字符, used for the numeric part; it indicates that the number is displayed, but the front and back 0 are not displayed.

data:02222.20pattern: ", ###.##"
Result:
2,222.2

#用于整数部分没有数位的限制, but used for fractional parts, there is "a maximum number of decimal places", and the extra rounding. Like what:

data:2222.222pattern: ", ###.##" Result:2,222.22

2. 0
The usage of 0 and # is similar, but slightly different; the difference is:
A. 0 limit the number of bits that appear at least in the integer portion; the previous 0

data:22.26
pattern: "0,000.00" Result:0,022.26

B. 0 limit the number of digits that appear in the decimal part; less than 0, more rounding

data:22.26
-----------------
pattern: "00.000" Result:22.260
-----------------
Pattern: "00.0"
Result:
22.3

3. E
Scientific notation, the mantissa and the exponent separator.
See the following article for details.

About prefixes, suffixes

Any Unicode character other than \ufffe, \UFFFF, and special characters can be prefixed and suffixed with 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.¤ (\U00A4)
Localized currency symbol, if two consecutive occurrences, represents the international designator of the currency symbol.

data:1234.567---------------------pattern:", ###.##¤"result:1,234.57¥--------------- ------Pattern:", ###.##¤¤"Result:1,234.57CNY

4. '
Used to refer to special characters as prefixes or suffixes.

data:1234.567pattern:"' # ', ###.##"result:#1, 234.57

If you want to use ' itself as a prefix or suffix, enter two consecutive times:

data:1234.567pattern:"', ###.##"result: '1,234.57
About scientific notation

The scientific notation is to add content such as E0 to the tail of the digital part, where E is the trailing and exponential delimiter, and the number of 0 after E represents the minimum number of digits of the exponent (less than 0 of the preceding).

Data:1e5pattern:0//  Note the index has 2 digits

E must have content in front of it, at least one # or 0, otherwise e is treated as a prefix:

data:1e5---------------pattern:"E0"result:e100000---------------
pattern:"0E0"result:1e5---------------
pattern:"#E0"result:.1e6

The index section does not have much content, and the following is a discussion of the tail part.

The mantissa generally includes integers and decimals, and if the integer part has no content (.), the valid numbers are all on the decimal:

Data:12345e5pattern:". # # #E0"result:.123e10

If you need to keep several numbers in the integer part, use a few 0 (the result of using # is a bit of a bizarre one, you try it yourself):

data:12345e5----------------------pattern:"0.## #E0"Result: 1.234E9----------------------pattern:"00.## #E0"result:12.345E8---------------------  Pattern:"000.## #E0"Result:123.45E7

Fractional part, if you want to achieve the "up to a few decimals" effect, in the pattern of the. Add the corresponding #. In this way, the excess is rounded, and if you want to achieve the effect of "just keeping a few decimals", add the corresponding 0 after the pattern. This way, the missing will be 0 in the back, and the excess will be 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

Formatting of Java floating-point types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.