Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused.......

In the following method:



I understand I use it should the balance change?

So then I use a getMethod:



I get the following error to screen:

C:\java\BankAccount>javac TestClass.java
.\Current.java:101: incompatible types
found : java.math.BigDecimal
required: double
this.newBalance = newAmount;
^
1 error

Nothing is going in my getBalance method though!!
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your newBalance variable is a BigDecimal and not a double. I notice the comment in the first method that says "change double newBalance to BigDecimal when working with finances".

You should use BigDecimal when precision is important, such as finances, but at that point it's to late as the value being passed in is a double. If there is any precision loss because of the nature of floating point variables it has already happened.

You should probably do something more like this.



P.S. If you should not put the amount into a double anywhere in the program. You should always deal with it as a String or BigDecimal. BigDecimal has a constructor that takes a String so you can store the value as a BigDecimal as soon as you get the value into your program.
[ March 05, 2005: Message edited by: Steven Bell ]
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven, many thanks for your reply.

Unfortunately the getBalance and setBalance signatures and parameters have been defined for me. i.e.

I have to use the following for getBalance and setBalance:

getBalance return type double argument name and data type is ( )
setBalance return type void argument name and data type is (double newBalance)

That's what I'm getting confused about.


As I am dealing with finances I need to use BigDecimal but how on earth do I implement that with these method constraints? They have also stated no additional methods unless absolutely necessary!!
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, you're about halfway screwed as far as I can tell. Using BigDecimal will keep you from having some precision loss during math operations, hopefully that won't be a problem.

You'll have to get a double from the BigDecimal so you will probably have to use Double.parseDouble and get the String from the BigDecimal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic