1 year ago

#328461

test-img

tommy-ling

toFixed()'s issues on pushing items into an array

In function giveChange I am returning a two-dimensional array with the 2 values being banknote and change. However, whenever toFixed() is added to limit the decimal places of a floating number, the values in the array become 0 (i think it's due to my function), as opposed to banknote and change value. When toFixed() is commented out, the values come back into the array, although the value of "diff" becomes a float with endless decimals. Also, Math.round() does not solve this issue.

Additionally, this is an issue for only numbers ending with 1, 3, 6, 8 at hundredth decimal, but not for others.

What is causing this?

Below is the logic. I know the math logic i wrote is hard to read so some explanation here.

  1. Suppose initial diff is 72.56. 72.56 is between notes 100 and 20. So it can be divided only by 20 without becoming a fraction. So the max number of 20's to divide 72.56 is 3, before turning it into a fraction. code "Math.ceil(((diff-cid[j][0])/cid[j][0]))" and "(1+((diff-cid[j][0])/cid[j][0]))" is to find out this max number.

  2. I compare the product of note*max with what I have in the cash drawer. Whatever less is what I go for (where I use Math.min).

  3. I deduct either the cash in register or note*max from the change (named diff in my code, where I wrote diff = diff - (.....)

In each step, I push [note, change value] into the array changeArr. In the process I also tried to convert the endless floating notes back to only 2 decimals, by using Number(Number.toFixed(2)).

function giveChange(diff, cid) { 
  let changeArr
  //This is the two-dimensional array 

  for(let i = cid.length - 1; i > 0; i--) {
    if(cid[i-1][0] <= diff && diff <= cid[i][0]) {
      changeArr = Array(cid.length - i).fill([0, 0])
      // creating arrays to maintain always 9 arrays in the outer array. 
      
        for(let j = i-1; j >= 0; j--) {

        if(!Number.isInteger(((diff-cid[j][0])/cid[j][0]))
        && ((diff-cid[j][0])/cid[j][0]) > 0) {
          changeArr.push([cid[j][0],Math.min(cid[j][1], cid[j][0]*Math.ceil(((diff-cid[j][0])/cid[j][0])))])

          diff = diff - Math.min(cid[j][1], cid[j][0]*Math.ceil(((diff-cid[j][0])/cid[j][0])))
          diff = Number(diff.toFixed(2))
          //This line is causing the problem

          
        } else if (Number.isInteger(((diff-cid[j][0])/cid[j][0])) 
        && ((diff-cid[j][0])/cid[j][0]) > 0) {
          changeArr.push([cid[j][0],Math.min(cid[j][1], cid[j][0]*(1+((diff-cid[j][0])/cid[j][0])))])

          diff = diff - Math.min(cid[j][1], cid[j][0]*(1+((diff-cid[j][0])/cid[j][0])))
          diff = Number(diff.toFixed(2))
          //This line is causing the problem


        } else if ( ((diff-cid[j][0])/cid[j][0]) <= 0) {
          // if change is smaller than the banknote, push 0 into the array and move on to the next banknote

          changeArr.push([cid[j][0], 0])

        }
      }
    }
  }
  return {changeArr, remainder: diff}
}


function checkCashRegister(price, cash, cid) {

  let diff = cash - price
  cid[0][0] = 0.01
  cid[1][0] = 0.05
  cid[2][0] = 0.1
  cid[3][0] = 0.25
  cid[4][0] = 1
  cid[5][0] = 5
  cid[6][0] = 10
  cid[7][0] = 20
  cid[8][0] = 100
  // Converting from string (e.g."PENNY") to num (e.g. 0.01)


  return giveChange(diff, cid)

}


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

javascript

floating-point

numbers

tofixed

0 Answers

Your Answer

Accepted video resources