R
refreegrata
Hi list. I'm a newbie with Java, just reading about BigDecimals.
I have a question. I can't get a correct rounding operation.
For example:
----------------------------------------------------------------------------------------------
new BigDecimal(0.705).setScale(2,RoundingMode.HALF_UP);
is 0.70
----------------------------------------------------------------------------------------------
And
----------------------------------------------------------------------------------------------
new BigDecimal(0.705).round(new MathContext(2, RoundingMode.HALF_UP));
is also 0.70
----------------------------------------------------------------------------------------------
However, with other values, like 1.705, the value is rounding to 1.71;
I don't know if this happens only with values between zero to one. I
still can't replicate the problem with greatest values. Maybe don't
happens, or maybe happens with other values too. To me is a problem
don't be sure about this.
Are better ways to rounding?
Cheers. Sorry for my english, because still is too ugly and rough.
P.D.: Using Java SE 6.
I have a question. I can't get a correct rounding operation.
For example:
----------------------------------------------------------------------------------------------
new BigDecimal(0.705).setScale(2,RoundingMode.HALF_UP);
is 0.70
----------------------------------------------------------------------------------------------
And
----------------------------------------------------------------------------------------------
new BigDecimal(0.705).round(new MathContext(2, RoundingMode.HALF_UP));
is also 0.70
----------------------------------------------------------------------------------------------
However, with other values, like 1.705, the value is rounding to 1.71;
I don't know if this happens only with values between zero to one. I
still can't replicate the problem with greatest values. Maybe don't
happens, or maybe happens with other values too. To me is a problem
don't be sure about this.
Are better ways to rounding?
Cheers. Sorry for my english, because still is too ugly and rough.
P.D.: Using Java SE 6.