Start New Discussion within our Software Development Community

Hey everyone,
I am having difficulty parsing digits in java. By that I mean that I am trying to make a program print out the number of bills the user recieves when he/she recieves change after there payment, (Cash register).
Here is the code i've created to do this for me, it runs perfectly but it always ends up giving out the wrong amount of change.


double change = payment - totalcost ;
double hundredbills = change/100;
change = change%100;
double fiftybills = change/50;
change = change%50;
double twentybills = change/20;
change = change%20;
double tenbills = change/10;
change = change%10;
double fivebills = change/5;
change = change%5;
double toonie = change/2;
change = change%2;
double loonie = change/1;
change = change%1;
double quarter = change/0.25;
change = change%0.25;
double dime = change/0.10;
change = change%0.10;
double nickel = change/0.05;
change = change%0.05;
double penny = change/0.01;
Each of these variables are then printed out with the system.out.println command.

What an expected result you want to see? And what are you seeing from your program?

One thing to be noted is that you may multiply the "payment" and "total" variable with 100 before you deal with them. Then you can use "int" instead of "double" to do the computation. The reason is that "double" is an estimate value and may be off by a very small value (epsilon) each time you compute (especially division). Oh and "penny" should never need to be computed if you already multiply by 100 because whatever left should be "penny" amount.

This article has been dead for over six months. Start a new discussion instead.