When a double is used for business operations, the double calculation loses precision. You can use BigDecimal for calculations.
[Java]View PlainCopyPrint?
- Import Java.math.BigDecimal;
- Import org.junit.Test;
- Public class testbigdecimal{
- @Test
- Public void Test () {
- Double a= 0.1 ;
- Double b= 0.2 ;
- System.out.println (A+B);
- BigDecimal a1=New BigDecimal ("0.1");
- BigDecimal b1=New BigDecimal ("0.2");
- System.out.println (A1+B1);
- BigDecimal c1=New BigDecimal ("0.23574");
- A1=a1.add (C1);
- A1=a1.setscale (1, Bigdecimal.round_down);
- SYSTEM.OUT.PRINTLN (A1);
- }
- }
Import Java.math.bigdecimal;import Org.junit.test;public class testbigdecimal{ @Test public void Test () { double a=0.1; Double b=0.2; System.out.println (a+b); BigDecimal a1=new BigDecimal ("0.1"); BigDecimal b1=new BigDecimal ("0.2"); System.out.println (A1+B1);//Do not write System.out.println (A1.add (B1));//correct BigDecimal c1=new BigDecimal (" 0.23574 "); A1=a1.add (C1); A1=a1.setscale (1,bigdecimal.round_down); SYSTEM.OUT.PRINTLN (A1); }}
The output results are as follows:
0.30000000000000004
0.3
0.3
The addition and subtraction of double cannot accurately calculate 0.3, while using BigDecimal can.
Of course, if you directly pass double to BigDecimal, you will find that not only can not solve the accuracy problem, but the accuracy of the completion. So, to ensure accuracy, we pass the string to it.
The Bigdecimal.setscale () method is used to format the decimal point
Setscale (1) indicates that a decimal number is reserved and is rounded by default
Setscale (1,bigdecimal.round_down) directly removes the extra decimal digits, such as 2.35 will become 2.3
Setscale (1,BIGDECIMAL.ROUND_UP) Carry processing, 2.35 becomes 2.4
Setscale (1,bigdecimal.round_half_up) rounded, 2.35 turned 2.4Setscaler (1,bigdecimal.round_half_down) rounded, 2.35 becomes 2.3, if 5 is down
. scale () precision value, that is, the number of digits after the decimal point (note: BigDecimal can improve accuracy by setscale, as long as the new value is larger than the original!)
BigDecimal can also be setscale to reduce accuracy. Because the new value is smaller than the original, so you must ensure that the original value of the bit after the decimal point is 0, only in this way can be set than the original small precision.
Example: The original value is: 4.1235648, want to set the scale to less than 7 will be wrong, if the original value is: 4.1235000, the scale is set to less than 4 bits will be wrong, and set to 4, 5, 6, 7 are no problem, set a larger, certainly will not be wrong)
Add (BigDecimal) BigDecimal the value in the object, and then returns the object.
Subtract (BigDecimal) subtracts the value from the BigDecimal object and returns the object.
Multiply (BigDecimal) multiplies the values in the BigDecimal object and returns the object.
Divide (BigDecimal) divides the values in the BigDecimal object, and then returns the object.
BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
Example:BigDecimal mdata = new BigDecimal ("9.655"). Setscale (2, bigdecimal.round_half_up);
----Results:-----mdata=9.66
ToString () Converts the numeric value of the BigDecimal object to a string.
Doublevalue () returns the value in the BigDecimal object as a double-precision number.
Floatvalue () returns the value in the BigDecimal object as a single-precision number.
Longvalue () returns the value in the BigDecimal object as a long integer.
Intvalue () returns the value in the BigDecimal object as an integer.
Finance Project Java Development _bigdecimal (solving computational accuracy issues)