Hi, I’m working on the cash register challenge. Presently I am trying to ‘sort’ my change to determine how much of each denomination to give out. However in one case I am having a rather strange FP result.
My sorting looks like this:
for (let x = 0; x < cid.length; x++) {
cashInDrawer.total += cid[1];
switch(x) {
case 0:
cashInDrawer.pennies = cid[1] / .01;
break;
case 1:
cashInDrawer.nickles = cid[1] / .05;
break;
case 2:
cashInDrawer.dimes = cid[1] / .10;
break;
case 3:
cashInDrawer.quarters = cid[1] / .25;
break;
case 4:
cashInDrawer.dollars = cid[1] / 1;
break;
case 5:
cashInDrawer.fives = cid[1] / 5;
break;
case 6:
cashInDrawer.tens = cid[1] / 10;
break;
case 7:
cashInDrawer.twentys = cid[1] / 20;
break;
case 8:
cashInDrawer.hundreds = cid[1] / 100;
break;
}
Which is all fine and dandy, however for ‘nickles’ instead of getting a count of ‘41’ I get result of 40.99999999999…
Now I get that this is floating point math… But really ? I know I could ‘force’ it with ceiling or toFixed but I just wondered is this to be expected or am I missing something here? All the other denominations work fine (produce int values).