2

Hello I'm currently following the Computing with C# and the .NET Framework book and I'm having difficulty on one of the exercises which is

Write a C# program to make change. Enter the cost of an item that is less than one dollar. Output the coins given as change, using quarters, dimes, nickels, and pennies. Use the fewest coins possible. For example, if the item cost 17 cents, the change would be three quarters, one nickel, and three pennies

Since I'm still trying to grasp c# programming the best method I came up with is using the while loop.

while(costOfItem >= 0.50)
            {
                costOfItem -= 0.50;
                fiftyPence++;
            }

I have these for each of the pences 20,10,5 etc.. I'm checking if the amount is greater than or equal to 50 pence, if so, i reduce 50 pence from the amount given by the user and add 1 to the fiftypence variable.

then it moves onto the next while loop, which I have for each pences. The problem is, somewhere along the line one of the loops takes away, lets say 20 pence, and the costOfItem becomes something like "0.1999999999999" then it never drops down to 0, which it should to get the correct amount of change.

Any help is appreciated, please don't suggest over complex procedures that I have yet covered.

user3216701
  • 41
  • 1
  • 5
  • What type of unit are you using for the money given? – Mindstormer Jan 16 '16 at 18:45
  • what datatype are you using for `costOfItem`? Sounds like its `float`, if that's true, you should try `decimal` instead. – mrtig Jan 16 '16 at 18:47
  • You need to use `decimal` to represent money. Floating point types like `double` and `float` are not appropriate and will result in these sort of issues. See [this related question](http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when). – Charles Mager Jan 16 '16 at 18:47

4 Answers4

5

Never use double or float for money operations. Use Decimal. For all other problems of calculation accuracy you have to use "Double Epsilon Comparison" like Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to

Community
  • 1
  • 1
Benjamin Abt
  • 1,730
  • 18
  • 33
0

Sounds to me you are using float or double as datatype for costOfItem.
float and double store their values in binary. But not all decimal values have an exact representation in binary. Therefore, a very close approximation is stored. This is why you get values like "0.1999999999999".

For money calculations, you should always use decimal, since they avoid those small inaccuracies.

You can read more about the difference in Jon Skeets awesome answer to Difference between Decimal, Float and Double in .NET?

Community
  • 1
  • 1
Domysee
  • 12,718
  • 10
  • 53
  • 84
0

If you do the calculation in cents, you can use integers and then you don't get into floating point rounding problems

  • this is called Mill Representation and is based on the Mill Currency. All operations are based on integer. 1000 Mill = 1 Dollar. – Benjamin Abt Jan 16 '16 at 18:51
0

Thank you all for the fast replies. The issue was that i used double instead of decimal the links provided have people explaining why it's wrong to do so in some cases. Using double should be avoided in arithmetic since apparently some float numbers do not have exact a binary representation, so instead of .23 it gives 0.2299999999999. Changing the variable to decimal fixed my issue

user3216701
  • 41
  • 1
  • 5