T.Hehe - 5 months ago 14

Java Question

I need to write a java program that computes change will be given for a sales transaction.

It can only dispense 20s, 10s, 5s, 1s, quarters, dimes, nickles, and pennies.

I make it and its all okay but i have a problem. When i put math.round some they go round up like 1.6 in 20euros and it gives wronge change. And if i put math.floor it give less after in coins. What im doing wrong?

Here is my code:

public class MyCurrencyCashier {

`public static void main (String [] args){`

float twentyEuro;

float tenEuro;

float fiveEuro;

float oneEuro;

float twentyFiveCent;

float tenCent;

float fiveCent;

float oneCent;

float change; //declares how much change is given in return

change = Float.parseFloat(args[0]); //read the change from the command line

twentyEuro = (change / 20);

if (twentyEuro >= 1) {

change = (change % 20);

System.out.println("€20 bill: " + Math.round(twentyEuro));

} else {

System.out.println("€20 bill: 0");

}

tenEuro = (change / 10);

if (tenEuro >= 1) {

change = (change % 10);

System.out.println("€10 bill: " + Math.round(tenEuro));

} else {

System.out.println("€10 bill: 0");

}

fiveEuro = (change / 5);

if (fiveEuro >= 1) {

change = (change % 5);

System.out.println("€5 bill: " + Math.round(fiveEuro));

} else {

System.out.println("€5 bill: 0");

}

oneEuro = (change / 1);

if (oneEuro >= 1) {

change = (change % 1);

System.out.println("€1 bill: " + Math.round(oneEuro) + "\n");

} else {

System.out.println("€1 bill: 0");

}

twentyFiveCent= (change / 0.25f);

if (twentyFiveCent >= 1) {

change = (change % 0.25f);

System.out.println("25_cent_coins: " + Math.round(twentyFiveCent));

} else {

System.out.println("25_cent_coins: 0");

}

tenCent = (change / 0.10f);

if (tenCent >= 1) {

change = (change % 0.10f);

System.out.println("10_cent_coins: " + Math.round(tenCent));

} else {

System.out.println("10_cent_coins: 0");

}

fiveCent = (change / 0.05f);

if (fiveCent >= 1) {

change = (change % 0.05f);

System.out.println("5_cent_coins: " + Math.round(fiveCent));

} else {

System.out.println("5_cent_coins: 0");

}

oneCent = (change / 0.01f);

if (oneCent >= 1) {

change = (change % 0.01f);

System.out.println("1_cent_coins: " + Math.round(oneCent));

} else {

System.out.println("1_cent_coins: 0");

}

}//end main

}// end class

I put 32.12 and i expect

€20 bill: 1

€10 bill: 1

€5 bill: 0

€1 bill: 2

25_cent_coins: 0

10_cent_coins: 1

5_cent_coins: 0

1_cent_coins: 2

But i get this:

€20 bill: 2

€10 bill: 1

€5 bill: 0

€1 bill: 2

25_cent_coins: 0

10_cent_coins: 1

5_cent_coins: 0

1_cent_coins: 2

Answer

You shouldn't have to worry about rounding these numbers at all; by default, if you divide two numbers and cast/coerce them to `int`

, they will automatically be the floor of the quotient.

In other words, if you perform `(int) (32.12/20)`

, you're going to get 1 back. This is accurate, since there is only the possibility of 1 20 euro bill existing in that amount. The same would be true if you did `(int) (39.99/20)`

, since only one 20 euro bill exists in that amount, too.

You can use this information to carve out the specific monetary amounts you need; more importantly, they do *not* need to be stored in a `float`

at all since they're whole numbers.

The below is a sample of where you can do with this. I leave the rest of the values as an exercise for the reader.

```
int twentyEuro;
float change;
change = Float.parseFloat(args[0]); //read the change from the command line
twentyEuro = (int) (change / 20);
change = change - twentyEuro * 20;
System.out.println("€20 bill: " + twentyEuro);
System.out.println(change);
```