Double Type Precision problem format settings

Source: Internet
Author: User

We know that the double type will involve precision loss during processing, for example, double d = 100*1.005; the result of result d is 100.49999999999999. How can this problem be solved by DecimalFormat, it is used to round the double type as follows: DecimalFormat df = new DecimalFormat (pattern); String nums = df. format (value); pattern :". 00 "value: 100 * 1.005nums value: 100.50 commonly used semicolons of pattern: #, #0.00 (why do I need to use 0 digits? #, because if df. format (0). If # is used, 0 is displayed as null and the result is. if 0 is used, 0.00 is displayed.) For the pattern setting, refer to the following: Symbol Location Localized? Meaning0 Number Yes Digit # Number Yes Digit, zero shows as absent. number Yes Decimal separator or monetary decimal separator-Number Yes Minus sign, Number Yes Grouping separatorE Number Yes Separates mantissa and exponent in scientific notation. need not be quoted in prefix or suffix .; subpattern boundary Yes Separates positive and negative subpatterns % Prefix or suffix Yes Multiply by 100 and show as percentage \ u2030 Prefix or suffix Yes Multiply by 1000 and show as per mille limit (\ u00A4) prefix or suffix No Currency sign, replaced by currency symbol. if doubled, replaced by international currency symbol. if present in a pattern, the monetary decimal separator is used instead of the decimal separator. 'prefix or suffix No Used to quote special characters in a Prefix or suffix, for example, "'#" formats 123 to "#123 ". to create a single quote itself, use two in a row: "# o '''clock ".

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.