The example in this article describes the operation method of BigDecimal in java. Share it with everyone for your reference. The specific analysis is as follows:
Since double and float are not precise enough, BigDecimal must be used when performing commercial calculations. BigDecimal object is created as follows:
Copy the code as follows: BigDecimal b = new BigDecimal("12.000001");
System.out.println(b);
The output result is: 12.000001;
BigDecimal can be passed in String and double when creating it, but it is best to use String. As for the reason, just look at the following code:
Copy the code as follows: BigDecimal b = new BigDecimal("12.000001");
System.out.println(b);
BigDecimal c = new BigDecimal(12.01);
System.out.println(c);
The running result is:
12.000001
12.0099999999999997868371792719699442386627197265625
It can be seen that the precision will be lost when double is passed in.
Other operations of BigDecimal are as follows:
Copy the code. The code is as follows: //Addition
public static BigDecimal add(String num1, String num2) {
BigDecimal bd1 = new BigDecimal(num1);
BigDecimal bd2 = new BigDecimal(num2);
return bd1.add(bd2);
}
//Subtraction
public static BigDecimal subtract(String num1, String num2) {
BigDecimal bd1 = new BigDecimal(num1);
BigDecimal bd2 = new BigDecimal(num2);
return bd1.subtract(bd2);
}
//multiplication
public static BigDecimal multiply(String num1, String num2) {
BigDecimal bd1 = new BigDecimal(num1);
BigDecimal bd2 = new BigDecimal(num2);
return bd1.multiply(bd2);
}
//division
public static BigDecimal divide(String num1, String num2, int i) {
BigDecimal bd1 = new BigDecimal(num1);
BigDecimal bd2 = new BigDecimal(num2);
// i is the number of digits to be retained, BigDecimal.ROUND_HALF_UP represents the rounding rule
return bd1.divide(bd2, i, BigDecimal.ROUND_HALF_DOWN);
}
It is necessary to emphasize division. The third parameter is whether to round off.
ROUND_HALF_DOWN means that 1 will not be entered when 5 is encountered, that is, 1.5->1;
ROUND_HALF_UP means that when 5 is encountered, 1 is entered, that is, 1.5->2;
But there are some things to note:
When we use ROUND_HALF_DOWN, copy the code as follows: System.out.println(this.divide("67.75", "5",4));
System.out.println(this.divide("67.75", "5",1));
System.out.println("-------");
System.out.println(this.divide("67.751", "5",4));
System.out.println(this.divide("67.751", "5",1));
The running results are as follows:
13.5500
13.5
-------
13.5502
13.6
I was confused. Logically the second result should be 13.5! Why did it become 13.6? After many tests, I thought about it and found that this rounding is not based on the shift after the precision digit and compares it with 5, but all the data after the precision digit is compared with 5. In other words: 13.5500 is accurate to one digit, then Use 0.0500 to compare with 5, and 13.5502 is accurate to one digit, then use 0.0502 to compare with 5. It is larger than 5, so it closes upward.
I hope this article will be helpful to everyone’s Java programming.