This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Homework Problem with int and %  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program prompts the user to enter the cost of an item and the amount tendered for payment. It displays two output windows.
1. Convert the change to whole cents, and use int / and % to get Dollars and Cents
2. Use / and % to get the numbers of each bill from the Dollars, coins from the Cents
3. Hint: Rounding can eliminate unwanted digits, and correct for a missing cent.

This is my program. I tried to do what I can do but there are some problems, namely the fact that the product comes out with a bunch of decimals places after a bunch of variables, but i can't seem to figure it out. I'm stuck like entering 17.25 for the cost of item and then entering 20.21 for tendering. I'm stuck. Please help

 
Bartender
Posts: 3320
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are seeing the classic problem with using doubles (or floats for that matter) to hold currency values. you should never do this, you should always use BigDecimal or store the value in the smallest currency unit ie cents in an int or long.
Read the following articles:
http://www.javaranch.com/journal/2003/07/MoneyInJava.html
http://javarevisited.blogspot.co.uk/2012/02/java-mistake-1-using-float-and-double.html
 
Tony Docherty
Bartender
Posts: 3320
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch.
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also suggest you read ItDoesntWorkIsUseless and TellTheDetails.
 
Alejandro Loaiza
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked out your links and I understand the code, I just don't understand how to apply it. I'm a real newbie and this was an assignment due yesterday and I'm crashing to understand this....so again, I saw the link, understand the code, but I don't know how or where to apply the code.
 
author
Sheriff
Posts: 23590
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alejandro Loaiza wrote:This program prompts the user to enter the cost of an item and the amount tendered for payment. It displays two output windows.
1. Convert the change to whole cents, and use int / and % to get Dollars and Cents
2. Use / and % to get the numbers of each bill from the Dollars, coins from the Cents
3. Hint: Rounding can eliminate unwanted digits, and correct for a missing cent.

This is my program. I tried to do what I can do but there are some problems, namely the fact that the product comes out with a bunch of decimals places after a bunch of variables, but i can't seem to figure it out. I'm stuck like entering 17.25 for the cost of item and then entering 20.21 for tendering. I'm stuck. Please help



The assignment seems to imply that it is the route to take is to use whole numbers -- probably to use ints or longs to represents total cents. There is also a hint that the value should be rounded, assuming while converting from floating point to whole numbers. And with ints and longs, it should solve the floating point inaccuracies that you are seeing.

Henry
 
Rancher
Posts: 2842
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Docherty wrote:You are seeing the classic problem with using doubles (or floats for that matter) to hold currency values. You should never
do this, you should always use BigDecimal or store the value in the smallest currency unit ie cents in an int or long.


I guess you never have to calculate so called "market values" of huge pension insurance portfolio's...
it all depends on what exactly the purpose is. In this case, calculating the change in different units,
we're not talking about numbers that run into the billions, so using doubles should be fine.

@Alejandro
I would like to point you to line 19. You round the change to whole units here. So, if the change
was, say, 7.58, you're now changing it to 8, losing the cents. Here is the place to start using integers and cents,
like

Then, from here, as the other repliers indicate, use '/ ' and '%'

Greetz,
Piet
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!