Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11164

Rounding values

From refreegrata <refreegrata@yahoo.com>
Newsgroups comp.lang.java.programmer
Subject Rounding values
Date 2012-01-10 04:43 -0800
Organization http://groups.google.com
Message-ID <913cfe41-9fe7-4a3e-8ce3-52d468d92a30@r5g2000yqc.googlegroups.com> (permalink)

Show all headers | View raw


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.

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Rounding values refreegrata <refreegrata@yahoo.com> - 2012-01-10 04:43 -0800
  Re: Rounding values Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-10 08:06 -0500
    Re: Rounding values Roedy Green <see_website@mindprod.com.invalid> - 2012-01-10 06:53 -0800
      Re: Rounding values Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-10 11:38 -0500
  Re: Rounding values Tom McGlynn <taqmcg@gmail.com> - 2012-01-10 05:17 -0800
    Re: Rounding values refreegrata <refreegrata@yahoo.com> - 2012-01-10 06:09 -0800

csiph-web