1. Practical significance
In real-world development, if you need to do a float or double accurate calculations (especially financial calculations ), it is not possible to use float or double directly (see the test results of the main method of the code below for a specific example), Need to use BigDecimal.
2. Code
PackageCom.xxx.util;ImportJava.math.BigDecimal;/*** floating point precision algorithm*/ Public classBigdecimalarithutil {Private Static Final intDiv_scale = 10;//Division Precision (Save 10 as a decimal number, except for endless) /**Fractional Exact addition*/ Public Static DoubleAddDoubleD1,DoubleD2) {BigDecimal BD1=bigdecimal.valueof (D1); BigDecimal Bd2=bigdecimal.valueof (D2); returnBd1.add (BD2). Doublevalue (); } /**Decimal Precision Subtraction*/ Public Static DoubleSubDoubleD1,DoubleD2) {BigDecimal BD1=bigdecimal.valueof (D1); BigDecimal Bd2=bigdecimal.valueof (D2); returnbd1.subtract (BD2). Doublevalue (); } /**Fractional Precision multiplication*/ Public Static DoubleMulDoubleD1,DoubleD2) {BigDecimal BD1=bigdecimal.valueof (D1); BigDecimal Bd2=bigdecimal.valueof (D2); returnbd1.multiply (BD2). Doublevalue (); } /**Fractional Precision Division*/ Public Static DoubleDivDoubleD1,DoubleD2) {BigDecimal BD1=bigdecimal.valueof (D1); BigDecimal Bd2=bigdecimal.valueof (D2); /** When there is no limit to rounding (there are many ways to deal with the number of values except for countless), 10 decimal places are reserved after the decimal point*/ returnbd1.divide (Bd2, Div_scale, bigdecimal.round_half_up). Doublevalue (); } Public Static voidMain (string[] args) {//Test AdditionSystem.out.println ("0.05+0.01=" +bigdecimalarithutil.add (0.05,0.01)); System.out.println ("0.05+0.01=" + (0.05+0.01)); //Test SubtractionSystem.out.println ("1.0-0.42=" +bigdecimalarithutil.sub (1.0,0.42)); System.out.println ("1.0-0.42=" + (1.0-0.42)); //Test MultiplicationSystem.out.println ("4.015*100=" +bigdecimalarithutil.mul (4.015,100)); System.out.println ("4.015*100=" + (4.015*100)); //Test DivisionSystem.out.println ("123.3/100=" +bigdecimalarithutil.div (123.3,100)); System.out.println ("123.3/100=" + (123.3/100)); }}View Code
3, the attention point
- The above program I use isbigdecimal.valueof (double x)To encapsulate the double type x into BigDecimal, view the source code as follows:
/** * Note: This is usually the best way to convert double and float to a bigdecimal */ public static BigDecimal valueOf (double val) { returnnew BigDecimal ( Double.tostring (Val)); }
View CodeIn this case, you first convert the double to stringand then use the following construction method to convert the string to BigDecimal
Public BigDecimal (String val)
This is the best way to do it . If you are using a method that converts a double or float directly to BigDecimal, that is, using the following constructor, you may not get the exact result. (See note)
/** * Note: The results of this constructor can is somewhat unpredictable. * The result of the constructor is sometimes inaccurate */public BigDecimal (double val)
View Code
- In practice, it is possible to use int and long for precise calculation of floating-point numbers (by multiplying the floating-point numbers first by the corresponding multiples into int (<=9 decimal digits) or long (<=18 decimal number ). After the calculation is divided by the previous multiples, the results are obtained, and the performance of the method will be higher, the specific int, long and bigdecimal each use see "Effective Java (second edition)" 48th.
- It should be pointed out that in the actual development, the problem of poor performance ofBigDecimal can be ignored, is the first choice of the accurate calculation of floating point number, and according to the previous article, if the floating point number after conversion of the integer greater than 18 bits, must also be used BigDecimal
Chapter Two accurate calculation of Java floating-point numbers