I don't understand how I am suppose to use the bigdecimal class. If i
do this:
BigDecimal total = new BigDecimal(0);
total.add(new BigDecimal(100));
total.add(new BigDecimal(112.222));
System.err.println("total: " +new Double(total.setScale(2,
java.math.BigDecimal.ROUND_HALF_UP).doubleValue()));
The output is zero. Why? Everytime you intialize the big decimal to
zero it won't function?
Oliver Wong - 31 Oct 2006 20:32 GMT
>I don't understand how I am suppose to use the bigdecimal class. If i
> do this:
[quoted text clipped - 6 lines]
> The output is zero. Why? Everytime you intialize the big decimal to
> zero it won't function?
BigDecimal is immutable. When you do total.add(new BigDecimal(100)),
you're not changing the value. Rather, a new BigDecimal object is created
and returned, but you're throwing away this new object. You should write
your code as:
total = total.add(new BigDecimal(100));
for example.
- Oliver
Mike Schilling - 31 Oct 2006 20:33 GMT
>I don't understand how I am suppose to use the bigdecimal class. If i
> do this:
[quoted text clipped - 6 lines]
> The output is zero. Why? Everytime you intialize the big decimal to
> zero it won't function?
BigDecimals are immutable, like strings.
Try::
total = total.add(new BigDecimal(100));
etc.
epicwinter@hotmail.com - 31 Oct 2006 20:41 GMT
banging head against wall...
thanks
> >I don't understand how I am suppose to use the bigdecimal class. If i
> > do this:
[quoted text clipped - 13 lines]
> total = total.add(new BigDecimal(100));
> etc.