Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
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:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Jeanne Boyarsky
• paul wheaton
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Stephan van Hulst
• Ron McLeod
• Tim Moores
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Vijitha Kumara

# Homework Problem with int and %

Greenhorn
Posts: 9
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: 3323
86
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.
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: 3323
86
And welcome to the Ranch.

lowercase baba
Posts: 12734
51
I would also suggest you read ItDoesntWorkIsUseless and TellTheDetails.

Alejandro Loaiza
Greenhorn
Posts: 9
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
Posts: 23821
140

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: 3121
110

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

 Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad. global solutions you can do at home or in your backyard https://www.kickstarter.com/projects/paulwheaton/better-world-boo