Solving Java.math.BigDecimal Divide method operation result is infinite decimal problem

Source: Internet
Author: User

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
    1. public static void Main (string[] args) {
    2. BigDecimal a = new BigDecimal ("10");
    3. BigDecimal o = new BigDecimal ("3");
    4. System.out.print (A.divide (o). Setscale (2, Bigdecimal.round_down). Doublevalue ());
    5. }

Java code
    1. Exception in thread "main" java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result.
    2. At Java.math.BigDecimal.divide (bigdecimal.java:1514)
    3. At Test.main (Test.java:8)

Workaround:
Java code
    1. public static void Main (string[] args) {
    2. BigDecimal a = new BigDecimal ("10");
    3. BigDecimal o = new BigDecimal ("3");
    4. System.out.print (A.divide (o,2, Bigdecimal.round_down). Doublevalue ());
    5. }

Output: 3.33


places to be aware of:
Java code
  1. /**
  2. * (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.
  3. * 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.
  4. * The purpose of designing BigInteger and BigDecimal is to accurately represent large integers and decimals, which are used in commercial calculations.
  5. * (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.
  6. * 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.
  7. * For example, new BigDecimal (0.1) Gets a value of 0.1000000000000000055511151231257827021181583404541015625.
  8. * 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!
  9. * (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.
  10. * 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 ().
  11. * (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 ....
  12. * For this reason, BigDecimal allows you to explicitly control rounding when you perform a division operation.
  13. */
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.