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
Read the following articles:
http://www.javaranch.com/journal/2003/07/MoneyInJava.html
http://javarevisited.blogspot.co.uk/2012/02/javamistake1usingfloatanddouble.html
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
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
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
Seriously? That's what you're going with? I prefer this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJIDEA
