0

When parsing large decimal numbers, javascript arbitrary rounds and loses some decimal places.

With a 'small' number, everything happens as expected:

var str = '1.1234567899'; -> undefined
console.log( Number(str) ); -> 1.1234567899

With a 'large' number, it's rounded and some data are lost:

var str = '123456789.1234567899'; -> undefined
console.log( Number(str) ); -> 123456789.12345679

Notice the diff between input and output: 123456789.1234567899 !== 123456789.12345679

I'm trying to understand why it happens. Anyone?

ByIvo
  • 41
  • 3
  • 1
    https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – ginginsha Jun 20 '17 at 19:35
  • Solution: Take a look in bignumber Math.js -> http://mathjs.org/examples/bignumbers.js.html – ByIvo Jun 20 '17 at 19:41
  • 1
    JavaScript doesn't have an integer value, any number is internally stored as double. So if the value goes to large it will lose some precision. The `Number.MAX_SAFE_INTEGER` constant represents the maximum safe integer in JavaScript (2^53 - 1). – Alinex Jun 20 '17 at 19:41

0 Answers0