2

I'm trying to convert "9999999999999099" into a number/float in javascript. The problem is that both parseFloat(X) and Number(X) return 9999999999999100.

This is not the same number. Does anyone know why this happens and how to avoid it? Thanks in advance.

cattox
  • 177
  • 2
  • 16
  • 1
    Beyond at most 9007199254740992 you cannot represent all numbers exactly with floating-point math. (That might not be the right number.) – Pointy Nov 13 '13 at 17:36

1 Answers1

4

Both numbers have the same floating point representation, because floating point numbers are only accurate to a certain level.

If these numbers are for calculation, why do you need to convert them with better than 1 in a quadrillion precision? If these values are not for calculation, leave them as strings.


By not for calculation, I mean something like a phone number or ID card number. It's meaningless to add or multiply phone numbers together.

rjmunro
  • 27,203
  • 20
  • 110
  • 132