The correct rounding method for Java

Source: Internet
Author: User

Original: http://www.cnblogs.com/yangzhilong/p/8405840.html

Run the following code in the case of JDK version 1.8, and you'll find a magical situation (see running results).

Look at the following code:

Package Com.longge.mytest;import Java.math.bigdecimal;import Java.math.roundingmode;import Java.text.DecimalFormat Import Org.junit.test;public class Testdecimal {@Test public void Test () {BigDecimal a = new BigDecimal (0).        075);        BigDecimal A1 = new BigDecimal (10.075);                BigDecimal A2 = new BigDecimal (100.075);        BigDecimal B = new BigDecimal (0.074);        BigDecimal B1 = new BigDecimal (10.074);                BigDecimal b2 = new BigDecimal (100.074);        BigDecimal C = new BigDecimal ("0.075");        BigDecimal C1 = new BigDecimal ("10.075");                BigDecimal C2 = new BigDecimal ("100.075");                DecimalFormat df = new DecimalFormat ("#0.00");                BigDecimal one = Bigdecimal.one;        0.07 System.out.println (A.divide (one, 2, roundingmode.half_up). Doublevalue ()); 0.08 System.out.println (A.divide (one). Add (New BigDecimal ("0.0000000001")). Setscale (2, roundingmode.half_up). Dou       Blevalue ()); 0.07 System.out.println (B.divide (one, 2, roundingmode.half_up). Doublevalue ()); 0.07 System.out.println (B.divide (one). Add (New BigDecimal ("0.0000000001")). Setscale (2, roundingmode.half_up). Dou        Blevalue ());        0.07 System.out.println (Df.format (0.074));        0.07 System.out.println (Df.format (0.075));                0.08 System.out.println (C.divide (one, 2, roundingmode.half_up). Doublevalue ());                System.out.println ("----------------------------");        10.07 System.out.println (A1.divide (one, 2, roundingmode.half_up). Doublevalue ()); 10.08 System.out.println (A1.divide (one). Add (New BigDecimal ("0.0000000001")). Setscale (2, roundingmode.half_up). D        Oublevalue ());        10.7 System.out.println (B1.divide (one, 2, roundingmode.half_up). Doublevalue ()); 10.07 System.out.println (B1.divide (one). Add (New BigDecimal ("0.0000000001")). Setscale (2, roundingmode.half_up). D Oublevalue ());        10.07 System.out.println (Df.format (10.074));        10.07 System.out.println (Df.format (10.075));                10.08 System.out.println (C1.divide (one, 2, roundingmode.half_up). Doublevalue ());                System.out.println ("----------------------------");        100.08 System.out.println (A2.divide (one, 2, roundingmode.half_up). Doublevalue ()); 100.08 System.out.println (A2.divide (one). Add (New BigDecimal ("0.0000000001")). Setscale (2, roundingmode.half_up).        Doublevalue ());        100.07 System.out.println (B2.divide (one, 2, roundingmode.half_up). Doublevalue ()); 100.07 System.out.println (B2.divide (one). Add (New BigDecimal ("0.0000000001")). Setscale (2, roundingmode.half_up).        Doublevalue ());        100.07 System.out.println (Df.format (100.074));        100.08 System.out.println (Df.format (100.075)); 100.08 System.out.println (C2.divide (one, 2, roundingmode.half_up). DouBlevalue ()); }}

The results of the operation are as follows:

0.07
0.08
0.07
0.07
0.07
0.07
0.08
----------------------------
10.07
10.08
10.07
10.07
10.07
10.07
10.08
----------------------------
100.08
100.08
100.07
100.07
100.07
100.08
100.08

Among the optional modes are:

1, Round_up: Rounding away from 0 directions. Rounds in the direction of the absolute value, as long as the discard bit is not 0.

2, Round_down: Trend 0 Direction rounding. Enter in the direction of the smallest absolute value, all the bits are discarded, there is no rounding.

3, Round_ceiling: Rounding to positive infinity. To the positive maximum direction. If positive, the rounding behavior is similar to round_up, and if negative, the rounding behavior is similar to Round_down. The Math.Round () method is the use of this pattern.

4. Round_floor: Rounding in negative infinity direction. To the negative Infinity direction. If positive, the rounding behavior is similar to Round_down, and if negative, the rounding behavior is similar to ROUND_UP.

5, Half_up: The nearest number is rounded (5 in). This is our most classic rounding.

6, Half_down: The most recent number rounding (5 homes). Here 5 is to abandon.

7, Hail_even: Banker rounding Method.

See the official description of the JDK:

Therefore, in the calculation of high accuracy requirements, the double/float will be converted into a string and then converted to BigDecimal for calculation

The correct rounding method for Java

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.