Re: Java rounding with big Decimal.
From: Russ Perry Jr (slapdash_at_rcn.com)
Date: 06/15/04
- Next message: Sudsy: "Re: Options for storing variables/information in a web application"
- Previous message: Russ Perry Jr: "Re: Options for storing variables/information in a web application"
- In reply to: Dale King: "Re: Java rounding with big Decimal."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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! || ++================================================================*//
- Next message: Sudsy: "Re: Options for storing variables/information in a web application"
- Previous message: Russ Perry Jr: "Re: Options for storing variables/information in a web application"
- In reply to: Dale King: "Re: Java rounding with big Decimal."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|