c# - Calculating the total amount of change given -


hello i'm following computing c# , .net framework book , i'm having difficulty on 1 of exercises

write c# program make change. enter cost of item less 1 dollar. output coins given change, using quarters, dimes, nickels, , pennies. use fewest coins possible. example, if item cost 17 cents, change 3 quarters, 1 nickel, , 3 pennies

since i'm still trying grasp c# programming best method came using while loop.

while(costofitem >= 0.50)             {                 costofitem -= 0.50;                 fiftypence++;             } 

i have these each of pences 20,10,5 etc.. i'm checking if amount greater or equal 50 pence, if so, reduce 50 pence amount given user , add 1 fiftypence variable.

then moves onto next while loop, have each pences. problem is, somewhere along line 1 of loops takes away, lets 20 pence, , costofitem becomes "0.1999999999999" never drops down 0, should correct amount of change.

any appreciated, please don't suggest on complex procedures have yet covered.

never use double or float money operations. use decimal. other problems of calculation accuracy have use "double epsilon comparison" double.epsilon equality, greater than, less than, less or equal to, greater or equal to


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -