Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

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

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

Tony Docherty

Bartender

Posts: 3271

82

posted 4 years ago

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

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

posted 4 years ago

I would also suggest you read ItDoesntWorkIsUseless and TellTheDetails.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

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.

posted 4 years ago

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

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

Piet Souris

Master Rancher

Posts: 2044

75

posted 4 years ago

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

Tony Docherty wrote:You are seeing the classic problem with using doubles (or floats for that matter) to hold currency values. You shouldnever

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