Rounding values

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.
 
E

Eric Sosman

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.

Try `new BigDecimal("0.705")' and similarly; note the "" marks.

I suspect what you're seeing is not caused by BigDecimal itself,
but by small approximation errors in the representation of the `double'
values. The system uses binary (base 2) for `double' numbers, and
just as there is no finite decimal representation of 3/7 there is no
finite binary representation of 705/1000 = 141/200. Unable to provide
an exact 705/1000, the system does its best by giving you a nearby
value with as small an error as it can manage, but it will not be
exactly halfway between 700/1000 and 710/1000. The damage has been
done before BigDecimal even arrives on the scene.
 
T

Tom McGlynn

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.


When the Java compiler sees the token 0.705 (or 1.705) it converts
that value into a double which may differ from the input by about 1
part in 10^15 -- most
decimal fractions cannot be represented exactly as doubles, so the
nearest available double value is used. You then convert this double
into a BigDecimal and finally round it. But your BigDecimal value is
not 0.705, it's actually something like 0.70499999999999... and so
properly gets rounded down.

To get the behavior you expect, get rid of the intermediate double by
converting
directly from a string to BigDecimal. E.g.,

BigDecimal x = new BigDecimal("0.705");

Now the BigDecimal value should be exactly what you specified.

Regards,
Tom McGynnn
 
R

refreegrata

Thanks guys, Within double quotes (") the problem is solved, and now I
understand why. Without the quotes is a double turned into a
BigDecimal.
 
E

Eric Sosman


An elaboration that begins with "My general rule is, if at
all possible, avoid floating point" may not be the best guide to
the subject. Would you trust dining advice from someone whose
general rule was to avoid food?

"Novice computer users solve this problem [floating-point
inaccuracy] by implicitly trusting in the computer as an
infallible authority; they tend to believe that all the
digits of a printed answer are significant. Disillusioned
computer users have just the opposite approach; they are
constantly afraid that their answers are meaningless."
-- DEK, TAOCP 4.2.2

Don't be a novice, but don't be so bitterly disillusioned, either.
Learn how the tools behave, and use them accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top