java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result exception workaround

Source: Internet
Author: User

Java program exception: java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result.
The statement that found the error is:

1 foo.divide(bar));

Originally in Java If you use BigDecimal to do division in the divide method must pass the second parameter, the definition of the number of decimal points, otherwise, in the case of not divisible, the result is an infinite loop decimal, the above exception will be thrown.
Workaround:

1 foo.divide(bar, 2, BigDecimal.ROUND_HALF_UP);

Note that this divide method has two overloaded methods, one for two parameters and one for three parameters:

Two parameters of the method:

@param divisor value by which this {@code BigDecimal} are to be divided. Incoming divisor

@param roundingmode rounding mode to apply. Mode of incoming round

Three parameters of the method:

@param divisor value by which this {@code BigDecimal} are to be divided. Incoming divisor
@param scale Scale of the {@code BigDecimal} quotient to be returned. Incoming precision
@param roundingmode rounding mode to apply. Mode of incoming round

Tonitech All rights reserved | Reprint Please specify source: http://www.tonitech.com/2327.html

java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result exception workaround

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.