Rounding Error In the Hundredths Place

Tell us what’s happening:
I’m working on the final JavaScript project, and I’m running into floating point rounding errors.

I’m trying to fix this by checking if the value for variable change is something like 0.009 (which is worth a penny and will need to be rounded up to 0.01 to get the correct answer), however as you can see in situations where the value of change is zero, and I specify not to round up unless it’s at least 0.005, it’s rounding up anyway.

I’m not sure if this is related to floating point errors, but I would appreciate an explanation on why this is happening.

Since my code has a lot of lines, here is the key section:

‘’’
if (0.005 <= change < 0.01){
console.log(‘yes, heres the change: ’ + change)
change = 0.01;
‘’’

``````  **Your code so far**
``````
``````
function checkCashRegister(price, cash, cid) {
let change = cash - price;
let finChange = change;
let undArr = [];
let hunArr = [];
let twenArr = [];
let tenArr = [];
let fiveArr = [];
let oneArr = [];
let quarterArr = [];
let dimeArr = [];
let nickleArr = [];
let pennyArr = [];
for (let i = 0; i < cid[8][1]/100; i++){
hunArr.push(100)
}
for (let i = 0; i < cid[7][1]/20; i++){
twenArr.push(20)
}
for (let i = 0; i < cid[6][1]/10; i++){
tenArr.push(10)
}
for (let i = 0; i < cid[5][1]/5; i++){
fiveArr.push(5)
}
for (let i = 0; i < cid[4][1]/1; i++){
oneArr.push(1)
}
for (let i = 0; i < cid[3][1]*4; i++){
quarterArr.push(0.25)
}
for (let i = 0; i < cid[2][1]*10; i++){
dimeArr.push(0.1)
}
for (let i = 0; i < cid[1][1]*20; i++){
nickleArr.push(0.05)
}
for (let i = 0; i < cid[0][1]*100; i++){
pennyArr.push(0.01)
}

let changeCount = [];

while (change >= 100){
changeCount = changeCount.concat(hunArr.pop())
change = change - 100
}
console.log(change)
while (change >= 20){
changeCount = changeCount.concat(twenArr.pop())
change = change - 20
}
console.log(changeCount)
console.log(change)
while (change >= 10){
changeCount = changeCount.concat(tenArr.pop())
change = change - 10
}
console.log(change)
while (change >= 10){
changeCount = changeCount.concat(tenArr.pop())
change = change - 10
}
console.log(change)
while (change >= 5){
changeCount = changeCount.concat(fiveArr.pop())
change = change - 5
}
console.log(change)
while (change >= 1){
changeCount = changeCount.concat(oneArr.pop())
change = change - 1
}
console.log(change)
while (change >= 0.25){
changeCount = changeCount.concat(quarterArr.pop())
change = change - 0.25
}
console.log(change)
while (change >= 0.25){
changeCount = changeCount.concat(quarterArr.pop())
change = change - 0.25
}
while (change >= 0.10){
changeCount = changeCount.concat(dimeArr.pop())
change = change - 0.10
}
console.log(change)
while (change >= 0.05){
changeCount = changeCount.concat(nickleArr.pop())
change = change - 0.05
}
while (change >= 0.01){
changeCount = changeCount.concat(pennyArr.pop())
change = change - 0.01
}
console.log('change ' + change)
if (0.005 <= change < 0.01){
console.log('yes, heres the change: ' + change)
change = 0.01;
}
console.log(change)
console.log(changeCount)
while (change >= 0.01){
changeCount = changeCount.concat(pennyArr.pop())
change = change - 0.01
}
console.log(changeCount)
console.log(change)
let changeArr = [];
for (let i = 0; i < changeCount.length; i++){
if (changeCount[i] != undefined){
changeArr = changeArr.concat(changeCount[i])
undArr = undArr.concat(1)
}
}
console.log(changeArr)

while (changeArr.length > 1){
changeArr[1] = changeArr[0] + changeArr[1]
changeArr.shift()
}
changeArr = changeArr.join('')
changeArr = parseFloat(changeArr)
console.log(changeArr)

console.log('finchange: ' + finChange)

finChange = finChange.toString()
let regex =/\d+\./
console.log(finChange)
console.log('change: ' + changeArr)

console.log(undArr)

if (changeArr < finChange){
return {status: "INSUFFICIENT_FUNDS", change: []};
}
if (changeArr == finChange){
if (undArr.length == 0){
return {status: "CLOSED", change: cid}
} else {
return {status: "OPEN"}
}
}

}

console.log(checkCashRegister(19.5, 129, [["PENNY", 0.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]]));
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15`

Challenge: Cash Register