aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with BigDecimal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with BigDecimal" Watch "Trouble with BigDecimal" New topic
Author

Trouble with BigDecimal

Paul Heubach
Greenhorn

Joined: Mar 21, 2011
Posts: 4
I am having difficulties with the fundamentals of BigDecimal.
Here is relevant code:
String strOpperand1, strOpperand2 ;
strOpperand1 = "546.0" ;
strOpperand2 = "218.4" ;
BigDecimal BD1 = new BigDecimal (strOpperand1) ;
BigDecimal BD2 = new BigDecimal (strOpperand2) ;
BD1.add (BD2) ;

When I run it, the value in BD1 is unchanged. How do I get it to add?

Thank you
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Hi Paul. Welcome to The Ranch!

You need to get the value returned from add(). BigDecimals are immutable, so you can't change the state of one.

It's similar to using Strings - you can't change the value of a String, but the class has a number of methods that return a new String with different values.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Paul Heubach wrote:I am having difficulties with the fundamentals of BigDecimal.
Here is relevant code:
String strOpperand1, strOpperand2 ;
strOpperand1 = "546.0" ;
strOpperand2 = "218.4" ;
BigDecimal BD1 = new BigDecimal (strOpperand1) ;
BigDecimal BD2 = new BigDecimal (strOpperand2) ;
BD1.add (BD2) ;

When I run it, the value in BD1 is unchanged. How do I get it to add?

Thank you


Take a look at the JavaDoc for BigDecimal -- specifically the add() method. You will notice that the result is returned as a new BigDecimal object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Heubach
Greenhorn

Joined: Mar 21, 2011
Posts: 4
Thank you

I added the following code:

BigDecimal BD3 = new BigDecimal ("0") ;
BD3 = BD1.add (BD2) ;


I will "play" with the other functions.

Paul
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Don't bother creating a BigDecimal just to throw it away. Just do this:

Or, you can reuse your bd1 variable, giving the "effect" of mutability:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

And if you ever need a BigDecimal("0") you can also use BigDecimal.ZERO - it has the same value but it's a cached object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Heubach
Greenhorn

Joined: Mar 21, 2011
Posts: 4
Thank you. I have changed my code to: BigDecimal BD3 = BD1.add (BD2) ;

I am getting a run time error with divide. I will look at the documentation. I may need to write back concerning divide run time.

Paul
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
For divide() you need to specify scale and rounding mode. There is a class called something like MathContext which incorporates both those values, and is probably better to use.
Paul Heubach
Greenhorn

Joined: Mar 21, 2011
Posts: 4
Thank you.

I did get the divide to work by adding an integer for the scale and an integer for the rounding mode. I will try math context.

Paul
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with BigDecimal