# Trouble solving Cash Register Project decimal accuracy!

Hi All!

I’m on the last problem of the data structures and algorithms cert, but I’m stuck on the math portion of the project! I’ve tried Math.round(), and I’ve also tried using (.100000000000000) for a penny and so on. I’ve read a few forums and they mentioned that this should work, but somehow I’m still getting inaccurate decimals.

Is anyone able to help me get precise decimals on this one? I’m not finished with the problem but I only have to make the return function after this. I’ve logged the customer’s change as well, so you can see what happens.

Would it be better to go another route? Thank you!!

``````function checkCashRegister(price, cash, cid) {

//Making currency amounts suitable for JS math
let penny = (Math.round(.01 * 100)/100); let nickel = (Math.round(.05 * 100)/100); let dime = (Math.round(.1 * 100)/100); let quarter = (Math.round(.25 * 100)/100); let one = (Math.round(1 * 100)/100); let five = (Math.round(5 * 100)/100); let ten = (Math.round(10 * 100)/100); let twenty = (Math.round(20 * 100)/100); let hundred = (Math.round(100 * 100)/100);

//Currency list to easily call in all functions
let currencyList =  [ ["PENNY", penny], ["NICKEL", nickel], ["DIME", dime], ["QUARTER", quarter], ["ONE", one], ["FIVE", five], ["TEN", ten], ["TWENTY", twenty], ["ONE HUNDRED", hundred] ];

let storage = []; let remainder = cash - price;

//Create subtractor to make sure all change is accounted for
let subtractor = (Math.round(remainder * 100)/100);
let cashRemainder = cid; let drawerCash = 0;
let change = [ ["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["ONE", 0], ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0] ];

// check to see if remainder is bigger than a certain currency and if it is available in the drawer (check)
// if so then push that amount to the change array and subtract it from subtractor (check)
// also subtract that amount from the cid (check)
// then repeat this process by calling this function again

//Adding up the money in the drawer
for (let c = 0; c < cid.length; c++){
drawerCash += cid[c];
};

console.log((subtractor).toPrecision(17));

//Deciding whether to go through the currencyType function
function decider (num) {
while (subtractor >= 0){
currencyType(subtractor);
}
};

//Decide whether or not enough change left
for (let l = 0; l < 1; l++){
if (drawerCash > remainder){
decider(Math.round(remainder *100)/100);
} else if (drawerCash >= remainder) {
decider(Math.round(remainder *100)/100);
} else {
return {status: "INSUFFICIENT_FUNDS", change: []};
}
}

//Seeing what type of currency needs to be used, subtracting it from the cash left in the drawer and the money left to give, and adding it to the customer's change
function currencyType (num){
if (num >= 100 && currencyList <= cashRemainder){
change += hundred;
subtractor = (subtractor - hundred);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= 20 && currencyList <= cashRemainder){
change += twenty;
subtractor = (subtractor - twenty);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= 10 && currencyList <= cashRemainder){
change += ten;
subtractor = (subtractor - ten);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= 5 && currencyList <= cashRemainder){
change += five;
subtractor = (subtractor - five);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= 1 && currencyList <= cashRemainder){
change += one;
subtractor = (subtractor - one);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= .25 && currencyList <= cashRemainder){
change += quarter;
subtractor = (subtractor - quarter);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= .1 && currencyList <= cashRemainder){
change += dime;
subtractor = (subtractor - dime);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= .05 && currencyList <= cashRemainder){
change += nickel;
subtractor = (subtractor - nickel);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
} else if (num >= .01 && currencyList <= cashRemainder){
change += penny;
subtractor = (subtractor - penny);
cashRemainder -= currencyList;
console.log(subtractor);
//changeDecider(subtractor);
}
};

console.log(change);

//round nums

}

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
``````

I would use an integer number of cents instead of a decimal number of dollars.

Hi Jeremy,

Thanks for that! The biggest problem I have is with the twenty. Since this is an integer already, this makes me think it won’t work. Did that work for you?

Why wouldn’t you be able to convert \$20 into an integer number of cents? There is an integer number of cents that go into \$20.

Since doing Math.round(20*100)/100 is still giving me an inaccurate decimal, how would I ago about getting a more accurate decimal?

This is what happens with Math.round(20*100)/100 when I’m doing the math for the change:

96.739999999999995
76.74
56.739999999999995
36.739999999999995

PS… I also rounded the remainder but it’s not showing up in the math.

That doesn’t convert to an integer number of cents. That converts to cents and immediately converts back to dollars.

1 Like

Ahhhhh I see! Im going to try that method now, thank you!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.