Java rounding to keep decimal number implementation method _java

Source: Internet
Author: User

Mode one:

Double F = 3.1516;
BigDecimal B = new BigDecimal (f);
Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();

Mode two:

New Java.text.DecimalFormat ("#.00"). Format (3.1415926);
#.00 represents a two-bit decimal #.0000 four-bit decimal, and so on ...

Mode three:

Double d = 3.1415926;
String result = String.Format ("%.2f", D);
%.2f%. Represents a floating-point type with a result of an arbitrary number of digits before the decimal point of 2 representing a two-bit decimal format.

Method Four:

Math.Round (5.2644555 *) * 0.01D;
String.Format ("%0" + + "D", 23) 23 less than 15 is on the front complement 0

The above is a small series for everyone to bring the Java rounding to keep the decimal points to achieve the full content of the method, I hope to help you, a lot of support cloud Habitat Community ~

Related Article

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.