• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with BigDecimal

 
Paul Heubach
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Heubach
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Heubach
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic