DecimalFormat Preserves decimal formatting

Source: Internet
Author: User
DecimalFormat Format method
Everyone in format () A decimal is, always on the format of the ' 0 ' and ' # ' some puzzled it.
eg
1:new DecimalFormat ("00.000"). Format (PI)/results: 03.142
2:new DecimalFormat ("##.###"). Format (PI)/results: 3.142
are formatted for PI, but the first result is 03.142, and the second result is 3.142.
What is the reason for that?
0 and # are placeholders, but they work differently in different places. The following is a concrete comparison of them.
Hope to help you.
0:
More than the actual number of digits, the shortage of places to fill up with 0.
New DecimalFormat ("00.00"). Format (3.14)//Result: 03.14
New DecimalFormat ("0.000"). Format (3.14)//Result: 3.140
New DecimalFormat ("00.000"). Format (3.14)//Result: 03.140
Less than the actual number of digits: The whole number of parts do not change, decimal parts, rounding
New DecimalFormat ("0.000"). Format (13.146)//Result: 13.146
New DecimalFormat ("00.00"). Format (13.146)//Result: 13.15
New DecimalFormat ("0.00"). Format (13.146)//Result: 13.15
#:
More than the number of digits in the actual number, unchanged.
New DecimalFormat ("##.##"). Format (3.14)//Result: 3.14
New DecimalFormat ("#.###"). Format (3.14)//Result: 3.14
New DecimalFormat ("##.###"). Format (3.14)//Result: 3.14
Less than the actual number of digits: The whole number of parts do not change, decimal parts, rounding
New DecimalFormat ("#.###"). Format (13.146)//Result: 13.146
New DecimalFormat ("##.##"). Format (13.146)//Result: 13.15
New DecimalFormat ("#.##"). Format (13.146)//Result: 13.15
Some other uses, (add percent semicolon, thousand semicolon, scientific notation, custom positive negative number template)
Can be referenced
http://jff.iteye.com/blog/576737
http://blog.csdn.net/marcoleung/article/details/176514
Above, if there is any irregularity, please point out.

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.