Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a decimal math library for JavaScript?

Is there a mature library for doing decimal-based math, possibly arbitrary-precision, in JavaScript?

Edit: I want this information for a reference page on floating-point-related problems and alternatives to use when binary floating-point is inappropriate: http://floating-point-gui.de/

like image 765
Michael Borgwardt Avatar asked Apr 12 '10 12:04

Michael Borgwardt


People also ask

Can you use decimals in JavaScript?

JavaScript has only one type of number. Numbers can be written with or without decimals.

How do you display decimals in JavaScript?

Use the toFixed() method in JavaScript to format a number with two decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.

How do I get 2 decimal places in JavaScript?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result.

Does JavaScript use floating-point math?

JavaScript supports one mathematical type, 64-bit floating point numbers.


1 Answers

As their author, I recommend bignumber.js or big.js, 'a small, fast Javascript library for arbitrary-precision arithmetic with decimal numbers'.

For a more mature library, the ICU4J BigDecimal translation is also recommended.

like image 97
MikeM Avatar answered Sep 19 '22 18:09

MikeM