Re: Java rounding with big Decimal.

From: Russ Perry Jr (slapdash_at_rcn.com)
Date: 06/15/04


Date: Mon, 14 Jun 2004 19:10:06 -0500

DaleKing@newsrvr.tce.com (Dale King) wrote:
> Russ Perry Jr wrote:
> > Christopher.fil.milburn@fidelity.com (ChrisMM) wrote:
> > > I have a double that I need to round to 6 dp, I thought I
> could just
> > > assign it to a BigDecimal, call its scale method and assign
> the result
> > > back to the double variable but something weird happens :-
> > > This code ...
> > >
> > > double db = 0.1234567890;
> > >
> > > System.out.print(" Double = "+ db );
> > > BigDecimal big = new BigDecimal( db );
> > > System.out.print(" big = "+big);
> > > System.out.print(" rounded big = "+big.setScale (6,
> > > BigDecimal.ROUND_HALF_UP));
> > > System.out.println(" back to double = " + new Double(
> > > big.doubleValue() ));
> > >
> > > produces this output...
> > >
> > > Double = 0.12345678900000004
> > > big =
> 0.1234567890000000389694179148136754520237445831298828125
> > > rounded big = 0.123457
> > > back to double = 0.12345678900000004
> > >
> > > The double somehow goes back to its original value ???
> > > Whats happening ? and how do I get the double to be the
> rounded
> > > amount.
> >
> > What you have to remember is that the double type is inherently
> > inaccurate...

> No it's not unless you mean it is innaccurate in the same way
> that any finite representation is.

That is, more or less, what I meant by "inherently inaccurate".

[explanation deleted here]

> > But generally speaking, get rid of the doubles if you need
> > the accuracy. It's a lot clumsier to use, but it's ACCURATE.

> What would you replac double with tha you consider accurate?
> BigDecimal is not really more accurate. It can have many more
> bits which means you can represent more numbers and it chooses to
> represent integral multiples of powers of 10 so it can represent
> fnite decimal numbers exactly. But is it really any more accurate
> when there are still an infinite number of values you can't
> represent?

Perhaps then I should say that it's as accurate as I've ever needed
it to be, and I suspect that given the approach I used in my posts
(which is how we've used it at work), it will handle the example
given by the original poster just fine.

Since it uses more bits to represent the numbers, you also accrue
less error in calculations involving them than you might with
doubles, IIRC. I still say BigDecimals are better than doubles,
at least in my experience of using them at work for insurance
calculations.

-- 
//*================================================================++
||  Russ Perry Jr   2175 S Tonne Dr #114   Arlington Hts IL 60005  ||
||  847-952-9729   slapdash@rcn.com [NEW!]   VIDEOGAME COLLECTOR!  ||
++================================================================*//


Relevant Pages

  • Re: C sucks at math? (help)
    ... I said I wouldn't try doubles in the real project, ... do similar calculations in a much bigger project and the problem I'm ... the floating point hardware or emulation does. ...
    (comp.lang.c)
  • Re: Advantage of using byte over Long Integer?
    ... J P Hill wrote: ... > I'm using an Access DB to do a series of calculations. ... > data are stored as doubles to maximise accuracy. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: double discrepancy between PDA and PC
    ... whatever you use from Math class uses doubles and that might ... calculations, ... the card reader takes electrical measurements off the pins with respect ... depending on the sensor and those are the values I use to do my ...
    (microsoft.public.dotnet.framework)
  • Re: Variable type Money/Currency
    ... By default, doubles cannot be ... > Do I need to take all my calculations and multiply them by 100? ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: operator overloading
    ... Daniel Dyer wrote: ... calculations that used BigDecimals, and they were a nightmare to maintain. ...
    (comp.lang.java.programmer)