Use bigdecimal for exact calculation

Source: Internet
Author: User
Tags mul

First, let's take a look at the following:CodeExample:

 1   Public   Class  Test_1 {  2       Public   Static   Void  Main (string [] ARGs ){  3 System. Out. println (0.06 + 0.01 );  4 System. Out. println (1.0-0.42 ); 5 System. Out. println (4.015*100 );  6 System. Out. println (303.1/1000 );  7   }  8       9 }

The running result is as follows.

0.06999999999999999

0.5800000000000001

401.49999999999994

0.30310000000000004

you think you are wrong, but the result is like this. Where is the problem? The reason is that our computer is binary. There is no way to accurately represent a floating point in binary format. Our CPU indicates that the floating point number is divided into two groups: : Index and tail number, such a representation method usually loses a certain degree of precision, and some floating point operations may also produce certain errors. Example: the binary representation of 2.4 is not exactly 2.4 . Instead, the nearest binary representation is 2.3999999999999999 . The value of a floating point is calculated by a specific mathematical formula.

JavaOfFloatIt can only be used for scientific computing or engineering computing. In most commercial computingJava. Math. bigdecimalClass.

In useBigdecimalClass, mainly divided into the following steps:

1. UseFloatOrDoubleVariable ConstructionBigdecimalObject.

2. CallBigdecimalAddition, subtraction, multiplication, division, and other corresponding methods for arithmetic operations.

3. Set bigdecimal convert an object to float , double , int and other types.

GenerallyBigdecimalConstruction method or static methodValueof ()Method to Construct a variable of the basic typeBigdecimalObject.

 
1Bigdecimal b1 =NewBigdecimal (double. tostring (0.48)));2Bigdecimal b2 = bigdecimal. valueof (0.48 );

, multiplication, division, bigdecimal the class provides the corresponding member methods.

  1   Public  bigdecimal add (bigdecimal value); ///   addition   2   Public  bigdecimal subtract (bigdecimal value); ///   subtraction   3   Public  bigdecimal multiply (bigdecimal value); ///   multiplication   4   Public  bigdecimal divide (bigdecimal value); ///   Division  

After calculation, we may needBigdecimalYou can useFloatvalue (),Doublevalue ().

The following is a tool class that provides addition, subtraction, multiplication, and Division operations.

 1   Public   Class  Arith {  2      /**  3   * Provides the add method for precise addition calculation.  4   *  @ Param  Value1 quilt count  5   *  @ Param  Value2 Addition  6   *  @ Return  The sum of the two parameters  7        */ 8       Public   Static   Double Add ( Double Value1, Double  Value2 ){  9 Bigdecimal b1 = New  Bigdecimal (double. valueof (value1 ));  10 Bigdecimal b2 = New  Bigdecimal (double. valueof (value2 ));  11          Return  B1.add (B2). doublevalue ();  12   }  13       14       /**  15   * Provides the sub Method for exact subtraction.  16   *  @ Param  Value1 subtrahend  17   *  @ Param  Value2 reduction 18   *  @ Return  Difference between two parameters  19        */  20       Public   Static   Double Sub ( Double Value1, Double  Value2 ){  21 Bigdecimal b1 = New Bigdecimal (double. valueof (value1 ));  22 Bigdecimal b2 = New  Bigdecimal (double. valueof (value2 ));  23           Return  B1.subtract (B2). doublevalue ();  24   }  25       26       /**  27   * Provides the Mul Method for exact multiplication.  28  *  @ Param  Value1 Multiplier  29   *  @ Param  Value2 Multiplier  30   *  @ Return  Product of Two Parameters  31        */  32       Public   Static   Double Mul (Double Value1, Double  Value2 ){  33 Bigdecimal b1 = New  Bigdecimal (double. valueof (value1 ));  34 Bigdecimal b2 = New  Bigdecimal (double. valueof (value2 ));  35           Return  B1.multiply (B2). doublevalue ();  36   } 37       38       /**  39   * Provides precise division calculation method Div  40   *  @ Param  Value1 Divisor  41   *  @ Param  Value2 Divisor  42   *  @ Param  Precise scale range 43   *  @ Return  Vendors of Two Parameters  44   *  @ Throws  Illegalaccessexception  45        */  46       Public   Static   Double Div ( Double Value1, Double Value2,Int Scale) Throws  Illegalaccessexception {  47           //  If the exact range is smaller than 0, an exception is thrown.  48           If (Scale <0 ){  49               Throw   New Illegalaccessexception ("accuracy cannot be less than 0" );  50   } 51 Bigdecimal b1 = New  Bigdecimal (double. valueof (value1 ));  52 Bigdecimal b2 = New  Bigdecimal (double. valueof (value2 ));  53           Return  B1.divide (B2, scale). doublevalue ();  54   }  55 }

 

 

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.