Double data reserved Two decimal places 1:5 six in

Source: Internet
Author: User

1  Packagecom;2 3  Public classT2 {4 5      Public Static voidMain (string[] args) {6 7System.out.println (Calculateprofit (0.233));8System.out.println (Calculateprofit (0.235));9System.out.println (Calculateprofit (0.237));TenSystem.out.println (Calculateprofit (0.2)); One  A     } -  -     /** the * Keep double type decimal after two bits, not rounded, directly take decimal after two bits such as: 10.1269 return: 10.12 -      *  -      * @paramDoublevalue -      * @return +      */ -      Public StaticString Calculateprofit (Doubledoublevalue) { +         //Keep 2 decimal places AJava.text.DecimalFormat DF =NewJava.text.DecimalFormat ("0.00"); atString result =Df.format (doublevalue); -         returnresult; -     } -  -}

Operation Result:

0.23
0.23
0.24
0.20

Other:

1: actually what I want to do is to round up, but after running the discovery unexpectedly is five to six into, if you have a good way to comment area tell me.

2:decimalformat for this type of hair:
DecimalFormat This type of formatted number is returned after the string, with the # number format, the number of bits is not enough when the 0, with 0 format the number of bits is not enough time to fill 0.

 

Related Blogs Double data holds two decimal digits two: Intercept decimal immediately after two bits without rounding

Double data reserved Two decimal places 1:5 six in

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.