# Cash Register Floating Points

**URL:** <https://forum.freecodecamp.org/t/cash-register-floating-points/334388>\
**Category:** General\
**Created:** [December 19, 2019, 12:26am UTC](https://forum.freecodecamp.org/t/cash-register-floating-points/334388 "2019-12-19T00:26:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bstefansen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bstefansen/32/143007_2.png) [@bstefansen](https://forum.freecodecamp.org/u/bstefansen)\
**Post date:** [December 19, 2019, 12:26am UTC](https://forum.freecodecamp.org/t/cash-register-floating-points/334388/1 "2019-12-19T00:26:29Z")

</div>

Hey y’all,  
This is my first post on the forums. I thought I figured out how to solve floating points by just multiplying my numbers in the beginning by 100 and dividing by 100 at the end. That seemed to work for each item on the checklist except for the last one. The PENNY value is 5 with a repeating decimal when it should just be 5. Does anyone see something I’m missing? Or is there a better way to deal with floating points?

```auto
function checkCashRegister(price, cash, cid) {
/* removes value from remainder, cid, and adds value to used array*/
  var valArr = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000]
  var remainder = (cash - price)*100;
  var used = [["PENNY", 0],["NICKEL", 0],["DIME", 0],["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 0],["TWENTY", 0],["ONE HUNDRED", 0]]
  cid.map(obj => obj[1] = obj[1]*100)
  for (let i = valArr.length - 1; i >= 0; i--) {
    while(valArr[i] <= remainder && cid[i][1] > 0) {
      remainder -= valArr[i];
      cid[i][1] -= valArr[i];
      used[i][1] += valArr[i]/100;
    }
  }
/* reshifts the used array */
  var newUsed = [];
  used.map(obj => newUsed.unshift(obj))
  var result = {
    status: "",
    change: ""
  }
/* checks remainder for results*/
  if(remainder == 0) {
    result.status = "OPEN"
    result.change = newUsed.filter(obj => !obj.includes(0))
  } else {
    result.status = "INSUFFICIENT_FUNDS";
    result.change = [];
  }
  return result;
}

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

/* result */
{ status: 'OPEN', change: [[ 'PENNY', 0.5000000000000002] ] }

```

---

<div class="post-metadata">

**Author:** ![snowmonkey](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snowmonkey/32/241579_2.png) [@snowmonkey](https://forum.freecodecamp.org/u/snowmonkey)\
**Post date:** [December 19, 2019, 2:34am UTC](https://forum.freecodecamp.org/t/cash-register-floating-points/334388/2 "2019-12-19T02:34:43Z")

</div>

The floating point bug is a thing, and it’s not merely a javascript thing. There are numerous solutions and suggestions, but I tend to go with the easy one - I simply convert the number, then convert it back:

```auto
const cents = 0.500000000002;
const roundedCents = Number(cents.toFixed(2) ); 
// cents.toFixed(2) gives me a string, rounded to two decimal places
// Number( ... ) converts that string BACK to a number, at my chosen precision.

```

---

<div class="post-metadata">

**Author:** ![bstefansen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bstefansen/32/143007_2.png) [@bstefansen](https://forum.freecodecamp.org/u/bstefansen)\
**Post date:** [December 19, 2019, 3:08am UTC](https://forum.freecodecamp.org/t/cash-register-floating-points/334388/3 "2019-12-19T03:08:03Z")

</div>

Thank you!  
I figured it out just as I read this. I came up with a different solution, but will post it for those who are stuck as well.

```auto
function checkCashRegister(price, cash, cid) {
/* removes value from remainder, cid, and adds value to used array*/
  var valArr = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]
  var remainder = cash - price;
  var used = [["PENNY", 0],["NICKEL", 0],["DIME", 0],["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 0],["TWENTY", 0],["ONE HUNDRED", 0]]
  cid.map(obj => obj[1] = obj[1])
  for (let i = valArr.length - 1; i >= 0; i--) {
    while(valArr[i] <= remainder && cid[i][1] > 0) {
      remainder -= valArr[i];
      cid[i][1] -= valArr[i];
      used[i][1] += valArr[i];
      used[i][1] = Math.round(used[i][1] * 100) / 100;
      remainder = Math.round(remainder * 100) / 100;
    }
  }
/* reshifts the used array */
  var newUsed = [];
  used.map(obj => newUsed.unshift(obj))
  var result = {
    status: "",
    change: ""
  }
  let noChange = cid.map(obj => obj[1] < 0).filter(obj => obj === true)
/* checks remainder for results*/
  if(remainder == 0 && noChange.includes(true)) {
    result.status = "CLOSED"
    result.change = used
  } else if(remainder == 0) {
    result.status = "OPEN"
    result.change = newUsed.filter(obj => !obj.includes(0))
  } else {
    result.status = "INSUFFICIENT_FUNDS";
    result.change = [];
  }
  return result;
}

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:30am UTC](https://forum.freecodecamp.org/t/cash-register-floating-points/334388/4 "2021-01-16T11:30:01Z")

</div>

Closed on January 16, 2021, 11:30am UTC


