1

i'm currently trying to store data via localstorage on my website, and if for example I do so :

localStorage.setItem("vue",10206726906969851)

When I want to get the value back I get this result :

localStorage.getItem("vue")
-> "10206726906969852"

So why does the value changes ? Thank you in advance for your help

saperlipopette
  • 1,603
  • 12
  • 26

1 Answers1

0

JavaScript is not epic at precision with numbers. An example:

.2 + .2 = .4

but

.2 + .2 + .2 = 0.6000000000000001

The number you are using is too big for JS to maintain good precision on it. Log the following in your console and you will see what I mean.

10206726906969851 > Number.MAX_SAFE_INTEGER // returns true

The number you are using is too big. I have experienced this in the past. The server will give me numbers that are fine for Java, but too large for JavaScript. So... JS will mess them all up. The only way to fix was to get a shorter number that JS wouldn't barf on.

frosty
  • 21,036
  • 7
  • 52
  • 74