Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

BigDecimal don't understand, doesn't add up right

Thread view: 
epicwinter@hotmail.com - 31 Oct 2006 20:22 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.