http://samueli.iteye.com/blog/224755
BigDecimal Division operation error, errors are as follows:
non-terminating decimal expansion; No exact representable decimal result
The reasons are:
BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
If divisor is zero, roundingmode==round_unnecessary and the specified are insufficient to represent The result of the division exactly
So when using divide, scale and roundingmode should be specified to ensure that there is sufficient range for the infinite decimal to represent the result.
Problem extension: The BigDecimal divide method results in an infinite number of decimal questions 10/3=3.3333333333333333 ...
Java code
- public static void Main (string[] args) {
- BigDecimal a = new BigDecimal ("10");
- BigDecimal o = new BigDecimal ("3");
- System.out.print (A.divide (o). Setscale (2, Bigdecimal.round_down). Doublevalue ());
- }
Java code
- Exception in thread "main" java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result.
- At Java.math.BigDecimal.divide (bigdecimal.java:1514)
- At Test.main (Test.java:8)
Workaround:
Java code
- public static void Main (string[] args) {
- BigDecimal a = new BigDecimal ("10");
- BigDecimal o = new BigDecimal ("3");
- System.out.print (A.divide (o,2, Bigdecimal.round_down). Doublevalue ());
- }
Output: 3.33
places to be aware of:
Java code
- /**
- * (1) BigInteger and BigDecimal are immutable (immutable), and when each step is performed, a new object is generated, since creating the object can incur overhead.
- * They are not suitable for a large number of mathematical calculations, should try to use basic types such as long,float,double to do scientific calculations or engineering calculations.
- * The purpose of designing BigInteger and BigDecimal is to accurately represent large integers and decimals, which are used in commercial calculations.
- * (2) There are 4 ways to make a bigdecimal, two of which are constructed with BigInteger, the other is constructed with a double, and one is constructed using string.
- * You should avoid using double to construct bigdecimal because: some numbers cannot be represented by a double at all, and are not accurate when passed to the BigDecimal construction method.
- * For example, new BigDecimal (0.1) Gets a value of 0.1000000000000000055511151231257827021181583404541015625.
- * The value given with new BigDecimal ("0.1") is 0.1. So, if you need a precise calculation, construct the BigDecimal with a string and avoid constructing it with a double, even though it looks simpler!
- * (3) The Equals () method considers that 0.1 and 0.1 are equal, returns True, and that 0.10 and 0.1 are unequal, and the result returns false.
- * Method CompareTo () is considered 0.1 and 0.1 equal, 0.10 and 0.1 are also equal. Therefore, when comparing two bigdecimal values from a numeric value, you should use CompareTo () instead of equals ().
- * (4) In other cases, fractional operations with arbitrary precision still do not represent precise results. For example, 1 divided by 9 will produce an infinite loop of decimals. 111111 ....
- * For this reason, BigDecimal allows you to explicitly control rounding when you perform a division operation.
- */
actual combat:Like what://Declaration D, either way the result is the same, the number is more than 8 bits is a problem, with the double length of theDouble d = 22722222.0;//double d = double.parsedouble ("22722222.0 ");//double d = double.valueof (" 22722222.0 ");//double d = new Double ("22722222.0 ");//Output D resultsSystem.out.println (d.tostring ());Results: 2.2722222e72.2722222e7! If the calculation condition is wrong, how can the calculation result be wrong?
Solving Java.math.BigDecimal Divide method operation result is infinite decimal problem