This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 4344
    
    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: 18546
    
  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: 4344
    
    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: 19656
    
  18

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: 38075
    
  22
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: 38075
    
  22
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with BigDecimal
 
Similar Threads
Double and Float
Issue adding double value
comparision between double and long
BigDecimal issue
Arithmetic operations with large numbers