So i'm working with java on a currency denominations. the code for the most part is working just fine, but the only issue is with pennies. the problem i'm having is that it will keep leaving out 1 single penny when inputting certain amounts. Apparently we are suppose to use math.round() to convert a double value to an equivalent long value that represents cents if we want precision....but the biggest problem I have is that I dont even understand what that means let alone even understanding how to implement math.round(). We are also given this....Math.round(amount * 100). I'm not expecting any answers, but if anyone can give me any clarity or directions you can point me in. I'd gladly appreciate it, for now i'm really confused and stuck.
import java.util.Scanner;
public class MoneySorter {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
double money;
System.out.printf("");
money = stdin.nextDouble();
int hundreds, fifty, twenties, tens, fives, ones;
int quarters, dimes, nickles, pennies;
hundreds = (int) money / 100;
money = money % 100;
fifty = (int) money / 50;
money = money % 50;
twenties = (int) money / 20;
money = money % 20;
tens = (int) money / 10;
money = money % 10;
fives = (int) money / 5;
money = money % 5;
ones = (int) money / 1;
money = money % 1;
quarters = (int) (money / 0.25);
money = money % 0.25;
dimes = (int) (money / 0.10);
money = money % 0.10;
nickles = (int) (money / 0.05);
money = money % 0.05;
pennies = (int) (money / 0.01);
Some examples would be if I put in the amount: 0.99
Result:
3 Quarters
2 Dimes
3 Pennies
example 2: 127.97
1 Hundred
2 Twenties
1 Five
2 Ones
3 Quarters
2 Dimes
1 Penny