2

Finding out the maximum 64bit int is easy - What is JavaScript's highest integer value that a number can go to without losing precision?

I want to know the maximum size the number can be given a certain amount of decimal places for currencies.

For instance:

0.99999999 // 8 decimal places
99999999.99999999 // accurate
999999999.99999999 // no longer accurate, becomes 1000000000

So for 8 places the number is "around" a billion.

How can I precisely know this number, and also for other decimal places like 2 and 4? Thanks

Dominic
  • 62,658
  • 20
  • 139
  • 163
  • 1
    Relevant to understand the topic: https://en.wikipedia.org/wiki/Floating-point_arithmetic for the basis, https://en.wikipedia.org/wiki/IEEE_754 for the specific standard that JS uses for floating point numbers. More specifically, it uses a 64-bit representation. Feel free to read other articles on the IEEE 754 standard or the standard itself. Play around with this tool to see how what you really have for each decimal number https://www.binaryconvert.com/convert_double.html For example `0.99999999` is actually `0.999999989999999949752407246706` which gets rounded. – VLAZ Oct 16 '20 at 06:26
  • Hm yeah good point, although `0.999999989999999949752407246706` is still precise enough that you could accurately round it to `0.99999999`. I'm wondering if there is either a cut off number where all numbers after become too imprecise for that, or if there is a calc you can perform to check a given number can be accurately rounded to 8 places without loosing precision – Dominic Oct 18 '20 at 16:09

0 Answers0