Ava Double: Rounding and setting the number of decimal places

Source: Internet
Author: User

public static void Main (string[] args) {   //1. First multiply and then round, then divide;           Double d = 62.31060027198647;          Double D2 = Math.Round (d*100)/100.0;         System.out.println ("Divide by math": "+ D2);          2. Rounding with the number of decimal places is accomplished by BigDecimal's Setscale (), which is converted to a BigDecimal object.       Below this feasible         BigDecimal bd = new BigDecimal (d);         BigDecimal Bd2 = Bd.setscale (2, bigdecimal.round_half_up);        System.out.println ("obtained by Bigdecimal.setscale:" + bd2);         3.         DecimalFormat df = new DecimalFormat ("#.##") of string returned by Decimalformat.format;         System.out.println ("obtained by Decimalformat.format:" + df.format (d));          4. Through String.Format         System.out.println ("Through StringFormat:" + String.Format ("%.2f", d));     

After the completion of math to do division: 62.31//Through the Bigdecimal.setscale obtained: 62.31//Through the Decimalformat.format obtained: 62.31//through the stringformat:62.31  

The second type of personal advice

Ava Double: Rounding and setting the number of decimal places

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.