Java rounded to reserved decimal places, java rounded to decimal places

Source: Internet
Author: User

Java rounded to reserved decimal places, java rounded to decimal places

// Method 1:
Double f = 3.1516;
BigDecimal B = new BigDecimal (f );
Double f1 = B. setScale (2, BigDecimal. ROUND_HALF_UP). doubleValue ();
 
// Method 2:
New java. text. DecimalFormat ("#. 00"). format (3.1415926 );
// #. 00 indicates two decimal places #. 0000 four decimal places and so on...
 
// Method 3:
Double d = 3.1415926;
String result = String. format ("%. 2f", d );
// %. 2f %. indicates any number of digits before the decimal point. 2 indicates that the result of two decimal places is f, indicating the floating point type.
 
// Method 4:
Math. round (5.2644555*100) * 0.01d;
// String. format ("% 0" + 15 + "d", 23) If 23 is less than 15, add 0 in front.

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.