0

I'm using Number() to convert an input into number and it is returning the wrong number.

Couldn't find a reason for this:

const aux = Number("3721250857372431234");

console.log(aux)

it will print 3721250857372431400 instead of 3721250857372431234

https://www.typescriptlang.org/play?#code/FAAhGMHsDsGcBcQEMCuAPEBeEA5FBbAIwFMAnACgCIBmAdgCYBGegVgAYAOF2u+gFmrNqfSgEoA3MFAQYsSABtiAOnmQA5uXgBPAA7FIAM3Ko0o0dKhwFy1RpOigA

mattdonsk
  • 39
  • 6
  • 2
    That number is larger than can be accurately represented by the Number type. If you need integers that large you may need to use [BigInts](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) – Nicholas Tower Sep 07 '22 at 15:58
  • 1
    This has almost nothing to do with TypeScript aside from being the language the problem was found in. Mind editing to change it to JavaScript? – kelsny Sep 07 '22 at 16:01

1 Answers1

1

Your number is bigger than the maximum safe integer and therefore the precision cannot be guaranteed.

JRose
  • 1,382
  • 2
  • 5
  • 18