1

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?

2
  • 1
    Turn it into a string, then use your own parser or a library that handles it, probably Commented Sep 4, 2020 at 3:05
  • Easiest way is to store and handle it as a string. Commented Sep 4, 2020 at 3:05

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.