Read about format double to 2 decimal places java, The latest news, videos, and discussion topics about format double to 2 decimal places java from alibabacloud.com
This article mainly introduces the Java method to keep the double type two decimal places, please refer to the use of itCopy CodeThe code is as follows:Mport Java.text.DecimalFormat;DecimalFormat df = new DecimalFormat ("##### #0.00");Double D1 = 3.2
Java: Rounding the double value to five, retaining the two decimal places, double rounding to five1. Functions
The double value in the program is accurate to the two digits after the decimal point. It can be rounded up or truncated.
For example, if
1. functionThe double value in the program is accurate to two digits after the decimal point. Can be rounded or truncated directly.For example: Enter 12345.6789, the output can be 12345.68 or it can be 12345.67. As to whether rounding is required,
Mode one:RoundedDouble F = 111231.5585;Rounding to keep two decimal digits, you can use the Format function of string,The method is as follows:
Copy Code code as follows:
System.out.println (String.Format ("%.2f", X1));
Reprint:http://blog.csdn.net/wj_j2ee/article/details/8560132Java retains two-bit decimal questions:Way One:RoundedDouble F = 111231.5585;BigDecimal B = new BigDecimal (f);Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();Keep two
Several methods are listed in this article:
1. Usejava.math.BigDecimal
2. Usejava.text.DecimalFormat
3. Usejava.text.NumberFormat
4. Usejava.util.Formatter
5. UseString.format
The end of the article to share more expansion of knowledge,
How to make float keep two decimal places or decimal placeshttp://meryvn.blog.163.com/blog/static/36962664201173010402629/Two methods:Import java.math.*;......Method 1:float F = 34.232323;BigDecimal B = new BigDecimal (f);Float F1 = B.setscale (2,
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.