• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

BigDecimal

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic