Ninja
Ninja

Reputation: 453

How to handle big numbers in Javascript?

I wanted to convert all my numbers to 4 fixed decimal points for that I am using toFixed()

 (1456.454534).toFixed()

But in my case I do have some number which are crossing the limit of MAX_SAFE_INTEGER like

11324237868788877767787.676886

In that case if I am using toFixed() it results into loss of precision.How can I handle this number?

Upvotes: 1

Views: 2435

Answers (1)

ben author
ben author

Reputation: 2955

Depending on your JS environment and compatibility requirements, you may have the new BigInt type available.

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

If using only the latest and greatest isn't an option, there is at least one polyfill library out there

... or sure, you could represent it as a string as some commenters suggest, but it's awkward to do math with those strings.

Upvotes: 3

Related Questions