aspose file tools*
The moose likes Beginning Java and the fly likes BigDecimal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal" Watch "BigDecimal" New topic
Author

BigDecimal

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Oct 04, 2004
Posts: 218
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

Joined: Dec 29, 2004
Posts: 1071
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BigDecimal