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


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

Re: Rounding values

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Rounding values
Date 2012-01-10 08:06 -0500
Organization A noiseless patient Spider
Message-ID <jehd5k$1sd$1@dont-email.me> (permalink)
References <913cfe41-9fe7-4a3e-8ce3-52d468d92a30@r5g2000yqc.googlegroups.com>

Show all headers | View raw


On 1/10/2012 7:43 AM, refreegrata wrote:
> 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.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next 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