round exponentially small number with .toFixed()

How can I round an exponentially small number like this:

2.4451778232910804e-26.toFixed(4) => 2.4452e-26

In docs, using .toFixed() is going to give me every time 0 . Is there a special function for exponentially small number ? I would rather not modify Number.prototype.toFixed() .


As you already wrote, toFixed isn't precise enough, as it only allows "only" up to 20 decimal places. Multiply + Dividing won't work as well, as the division might give you an inaccurate longer number again. But toPrecision(<amount-of-precision>) might help.

edit: If you want 4 decimal places, you need pass 5 as parameter (as the numbers before the point count as well).

toPrecision will give you a String, but you may easily cast it back to a number again, if needed. eg Number(someNumberAsString)

var someNumber = 2.4451778232910804e-26;
console.log(someNumber);
console.log(someNumber.toPrecision(8));
链接地址: http://www.djcxy.com/p/93186.html

上一篇: 仅将Google智能锁添加到网站

下一篇: 用.toFixed()指数级小数字