简体   繁体   English

用JavaScript除法后得到数字中的所有小数位

[英]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. 由于在除,乘,加或减之后,JavaScript倾向于减少显示的小数位数。 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. 如果您需要比JavaScript Number的精度更高的精度(大约16位数字),则需要使用bignumbers。

With math.js you can do something like: 使用math.js,您可以执行以下操作:

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 0.6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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