Handle javascript numbers with digits larger than 16

2.6k Views Asked by At

I tried looking on the forum on how to handle digits larger than 16. So far I found this post:

How can I handle numbers bigger than 17-digits in Firefox/IE7?

which does not provide a solution but only recommends to do some tricks to handle the operations.

I also looked online for libraries that could cope with this, such as this library, which is again limited by 15 digits.

http://mikemcl.github.io/decimal.js/#precision

What other ways are there to handle arithmetic operations for numbers with digits larger than 15?

1

There are 1 best solutions below

0
On BEST ANSWER

Try looking for decimal math and arbitrary precision libraries.

"Decimal math" means it operates on decimal digits instead of binary digits.

Decimal floating point (DFP) arithmetic refers to both a representation and operations on decimal floating point numbers. Working directly with decimal (base 10) fractions can avoid the rounding errors that otherwise typically occur when converting between decimal fractions (common in human-entered data, such as measurements or financial information) and binary (base 2) fractions.

Arbitrary precision means without hard limits on number of digits.

In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are limited only by the available memory of the host system. This contrasts with the faster fixed-precision arithmetic found in most arithmetic logic unit (ALU) hardware, which typically offers between 8 and 64 bits of precision.

Is there a decimal math library for JavaScript? might be a good starting point.