Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11166
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Rounding values |
| Date | Tue, 10 Jan 2012 08:06:58 -0500 |
| Organization | A noiseless patient Spider |
| Lines | 36 |
| Message-ID | <jehd5k$1sd$1@dont-email.me> (permalink) |
| References | <913cfe41-9fe7-4a3e-8ce3-52d468d92a30@r5g2000yqc.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 10 Jan 2012 13:07:00 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="HSlJAUb3pGXi3i7ZL/HoAw"; logging-data="1933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vxDfqdOfu1kaURFZjTldz" |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 |
| In-Reply-To | <913cfe41-9fe7-4a3e-8ce3-52d468d92a30@r5g2000yqc.googlegroups.com> |
| Cancel-Lock | sha1:8IGXGwUFDITFM+SwKnJtPSwY3jQ= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11166 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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