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)