BigDecimal class
For numbers that do not require any accurate calculation accuracy, float or double can be used directly, but if you need to calculate the results accurately, you must use the BigDecimal class, and you can use the BigDecimal class for a large number of operations. The common methods of the BigDecimal class are shown in table 11-15.
Table 11-15 Common methods of BigDecimal class
|
Serial number |
Method |
Type |
Description |
|
1 |
Public BigDecimal (double val) |
Structure |
Convert a double representation
To BigDecimal |
|
2 |
Public BigDecimal (int val) |
Structure |
Converts an int representation to a
BigDecimal |
|
3 |
Public BigDecimal (String val) |
Structure |
Represents a String
Convert form to BigDecimal |
|
4 |
Public BigDecimal Add (BigDecimal augend) |
Ordinary |
Addition |
|
5 |
Public BigDecimal Subtract (BigDecimal
subtrahend) |
Ordinary |
Subtraction |
|
6 |
Public BigDecimal Multiply (BigDecimal
Multiplicand) |
Ordinary |
Multiplication |
|
7 |
Public BigDecimal Divide (BigDecimal
Divisor |
Ordinary |
Division |
Example: Arithmetic for rounding
Package Org.lxh.demo11.numberdemo; Import Java.math.BigDecimal; Class MyMath {public static double add (double D1, double D2) {//For addition operation BigDecimal B1 = new BigDecimal (D1); BigDecimal b2 = new BigDecimal (D2); Return B1.add (B2). Doublevalue (); } public static double sub (double D1, double D2) {//subtraction operation BigDecimal B1 = new BigDecimal (d 1); BigDecimal b2 = new BigDecimal (D2); Return B1.subtract (B2). Doublevalue (); } public static Double Mul (double D1, double D2) {//multiplication BigDecimal B1 = new BigDecimal (d 1); BigDecimal b2 = new BigDecimal (D2); Return b1.multiply (B2). Doublevalue (); } public static double div (double d1, double D2,int len) {//divide operation BigDecimal B1 = new BigDecimal (d 1); BigDecimal b2 = new BigDecimal (D2); Return B1.divide (B2,len,bigdecimal. ROUND_HALF_UP). Doublevalue (); } public static double round (double d, int len) {//rounding operation BigDecimal B1 = new BigDecimal (d); BigDecimal b2 = new BigDecimal (1); Any number divided by 1 is the original number//ROUND_HALF_UP is a constant of bigdecimal that represents the rounding operation return B1.divide (B2, Len,bigdecim Al. ROUND_HALF_UP). Doublevalue (); }} public class BigDecimalDemo01 {public static void main (string[] args) {System.out.println (" Addition operation: "+ Mymath.round (Mymath.add (10.345, 3.333), 1)); System.out.println ("multiplication:" + mymath.round (Mymath.mul (10.345, 3.333), 3)); SYSTEM.OUT.PRINTLN ("Division operation:" + mymath.div (10.345, 3.333, 3)); System.out.println ("Subtraction operation:" + Mymath.round (mymath.sub (10.345, 3.333), 3)); } }
BigDecimal is a class in Java that represents an arbitrary precision floating-point number operation, and in BigDecimal, the Unscaledvaluex10-scale is used to represent a floating-point number. Where Unscaledvalue is a biginteger,scale is an int. From this point of view, BigDecimal can only identify finite decimals, but can represent a range of data far greater than double, in practical applications is basically enough.
Let's mention two accuracy questions:question one: BigDecimal's accuracy problem (a guy on StackOverflow asked a related question)
System.out.println (New BigDecimal (0.1). ToString ()); 0.1000000000000000055511151231257827021181583404541015625system.out.println (New BigDecimal ("0.1"). ToString ()) ; 0.1system.out.println (New BigDecimal (Double.tostring ( 0.1000000000000000055511151231257827021181583404541015625)). toString ());//0.1system.out.println (New BigDecimal ( Double.tostring (0.1)). ToString ()); 0.1
Analyze the problem with the above code (the contents of the comment represent the output of this statement)
The first line: In fact, since the binary cannot accurately represent decimal decimal 0.1, but after the compiler reads the string "0.1", it must be converted to a double value of 8 bytes, so The compiler can use only one nearest value instead of 0.1, or 0.1000000000000000055511151231257827021181583404541015625. Thus, at run time, the true value passed to the BigDecimal constructor is 0.1000000000000000055511151231257827021181583404541015625. Second line: BigDecimal can correctly convert the string into a truly accurate floating-point number. The third line: The problem is that Double.tostring uses a certain amount of precision to round the double and then output it. Yes. The double.tostring (0.1000000000000000055511151231257827021181583404541015625) output is actually "0.1", So the number of BigDecimal represented is also 0.1. Line four: Based on the previous analysis, the fact that this line of code is equivalent to the third line conclusion: 1. If you want BigDecimal to be able to accurately represent the value you want, be sure to use a string to represent the decimal and pass it to the BigDecimal constructor. 2. If you use double.tostring to convert a double into a string and then call BigDecimal (String), this is also not reliable, it does not necessarily work as you think. 3. If you are not very concerned about whether you are fully accurate and use BigDecimal (double), pay attention to the special case of the double itself, the specification of the double itself defines several specific double values (Infinite,-infinite,nan ), do not pass these values to BigDecimal, or you will throw an exception.question two: Is it not to throw away the fractional part by forcing the double into int?
int x= (int) 1023.99999999999999; x=1024 Why?
The reason is that the binary cannot accurately represent some decimal decimals, so the double value of 1023.99999999999999 after compilation becomes 1024.
So, forcing a double into int is really throwing away the fractional part, but the value you write in the code is not necessarily the true double value that the compiler generates. Verification Code:
Double d = 1023.99999999999999;int x = (int) d; System.out.println (New BigDecimal (d). toString ()); 1024system.out.println (Long.tohexstring (double.doubletorawlongbits (d))); 4090000000000000SYSTEM.OUT.PRINTLN (x); 1024
As mentioned above, BigDecimal can be accurately represented by a double, remember.
We can also print the binary form of D directly, according to IEEE 754, we can calculate the 0x4090000000000000= (1024).
BigDecimal high-precision computing familiarity with extensions, Java Division preserves decimal issues