Double rounding in Java and setting the number of decimal places

Source: Internet
Author: User

This article is reproduced, the original address: http://blog.csdn.net/star_huang/article/details/7639267

Today you have an issue that requires you to keep a double of the decimal point for the data of type two. Centralized solution for online search, copy come on, spare

First Result_value is the double before processing

Get_double is used to save my processed double

//programme One:Get_double = (Double) (Math.Round (result_value*100)/100.0) //Scenario Two:DecimalFormat DF =NewDecimalFormat ("#.##"); Get_double=double.parsedouble (Df.format (Result_value));//Programme III:get_double = double.parsedouble (String.Format ("%.2f", Result_value));//Programme IV:BigDecimal BD =NewBigdecimalresult_value (); BigDecimal Bd2= Bd.setscale (2, BigDecimal. ROUND_HALF_UP); Get_double= Double.parsedouble (Bd2. ToString ());

PS: I solved the problem with the second solution.

Other

 Public Static int Scale ) {        double result = 0.0;         if (null ! = value)            {new  BigDecimal (string.valueof (value)). Setscale (Scale, ROUNDINGMODE.HALF_UP). Doublevalue ();        }         return result;
}

This method allows you to set the number of digits that are retained after the decimal point.

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.