Non-terminating decimal expansion; no exact representable decimal result

Source: Internet
Author: User

Non-terminating decimal expansion; no exact representable decimal result-Lopper column-blog channel-csdn. Net http://blog.csdn.net/lopper/article/details/5314686

 

Because it is necessary to process floating-point numbers with relatively high precision, double type should be discarded, and bigdecimal type should be used for numerical processing.

There is no problem in addition, subtraction, multiplication, but the following error is prompted during division operation:

The approximate meaning is "Division expressions that cannot end; there is no precise division result ". I entered 10/3 at the time and the result should be 3. 3333... 333.

 

So I found a problem with my original processing method when I found the information on the Internet.

Bigdecimal num1 = new bigdecimal ("10 ");

Bigdecimal num2 = new bigdecimal ("3 ");

Bigdecimal num3 = num1.divide (num2 );

 

In fact, devide functions are defined as follows:

Bigdecimal. Divide (bigdecimal divisor, int scale, roundingmode );

Scale indicates the number of decimal places;

Roundingmode: decimal mode;

Round_ceiling
If bigdecimal is positive, the round_up operation is performed. If bigdecimal is negative, the round_down operation is performed.
Round_down
Never add a number before dropping (that is, truncating) decimal places.
Round_floor
If bigdecimal is positive, round_up is performed. If bigdecimal is negative, round_down is performed.
Round_half_down
If you discard part>. 5, round_up; otherwise, round_down.
Round_half_even
If the number on the left of the discard part is an odd number, round_half_up is used. If it is an even number, round_half_down is used.
Round_half_up
If you discard part> =. 5, round_up is performed. Otherwise, round_down is performed.
Round_unnecessary
The "pseudo-rounding mode" indicates that the required operation must be accurate, so no round-robin is required.
Round_up
The number is always added before the decimal point (that is, truncation) is not discarded by 0.

You can use

Bigdecimal num3 = num1.divide (num2, 10, round_half_down );

Non-terminating decimal expansion; no exact representable decimal result (convert)

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.