简体   繁体   中英

Get all decimal places in a number after division in JavaScript

Since after dividing, multiplying, adding, or subtracting, JavaScript tends to cut down the number of decimal places shown as a result. Is there a way to get ALL of the decimal places? If there isn't a simple way to do this, is there a library that can do so?

If you need a higher precision than the precision of the JavaScript Number (about 16 digits), you will need to use bignumbers.

With math.js you can do something like:

var math = mathjs({decimals: 100});
var a = math.bignumber(2);
var b = math.bignumber(3);
var c = math.divide(a, b);

console.log(math.format(c));

This will output:

0.6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM