1.
Example: What is the value of 3-2.6==0.4? Perhaps at first glance, the feeling is to return true because 3-2.6=0.4,0.4==0.4;
However, the above analysis is wrong in Java.
Because the operation of floating-point numbers is imprecise in Java, 3-2.6=0.39999999999999 can be seen, so it is clear that 3-2.6==0.4 returns false.
The question is, why are floating-point numbers not accurate in Java?
A preliminary understanding is related to the representation of floating-point numbers in a computer.
(using BigDecimal, a parameter is a string type, and a parameter is the difference between a double type)
After questioning the Niang, know: "This real number by an integer or fixed-point number (that is, the mantissa) multiplied by a cardinality (usually 2 in the computer) of the whole power to get, this representation is similar to the base of 10 scientific notation.
"," The problem of precision is thus generated, many of the numbers are not fully accurate in the finite n, we can only use a larger n value to more accurately represent this number
As a result, the expression of floating-point numbers is not the most accurate, then, it is obvious that the results of the operation of floating-point numbers are not inaccurate results.
And the solution to this problem (that is, how can floating point numbers be more accurate?) ) is to use the BigDecimal class.
Note Initialize with a parameter of type string.
Here you'll learn about the BigDecimal class:
There are altogether 4 construction methods of the BigDecimal:
BigDecimal (int) Creates an object with the integer value specified by the parameter.
BigDecimal (double) creates an object with the double value specified by the parameter.
BigDecimal (long) creates an object with a long integer value specified by the parameter.
BigDecimal (String) creates an object that has the numeric value specified by the parameter as a string.
BigDecimal operation does not support +-*/This type of operation it has its own method of operation:
BigDecimal Add (BigDecimal augend) addition operation
BigDecimal Subtract (BigDecimal subtrahend) subtraction operation
BigDecimal Multiply (BigDecimal multiplicand) multiplication operation
BigDecimal Divide (BigDecimal divisor) Division operation "
2.
Re-read the teacher's courseware, found that this stuff is very useful.
Java knowledge learned by 16.10.17