0

During development work on a project, we encountered some strange behavior. We implemented a REST route using the NodeJs framework in version 14. The route accepts a query parameter. When passing the value "9995151671798245" as a string and then parsing it as an integer, we noticed that the result is the integer value 9995151671798244. So the string got subtracted by a value of one.

I was able to reproduce this issue with the following console output:

// Wrong ?
console.log(["9995151671798245", parseInt("9995151671798245", 10), Number("9995151671798245"), Math.ceil("9995151671798245"), parseFloat("9995151671798245")])
// Correct!
console.log(["9995151671798244", parseInt("9995151671798244", 10), Number("9995151671798244"), Math.ceil("9995151671798244"), parseFloat("9995151671798244")])
// Correct!
console.log(["9995151671798246", parseInt("9995151671798246", 10), Number("9995151671798246"), Math.ceil("9995151671798246"), parseFloat("9995151671798246")])

The surrounding numbers are also working as expected. How can it be that the value is subtracted by one during parsing?

Andre
  • 1
  • 3
  • 1
    See [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) ... – AKX Dec 27 '22 at 09:56
  • The other commenters are right, you should use [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) instead. – Mehmet Baker Dec 27 '22 at 09:58
  • Ahh alright, thank you that helped! Did not know that there is a value of MAX_SAFE_INTEGER – Andre Dec 27 '22 at 10:11

0 Answers0