在javascript中将指数表示法小数位限制为4
如何在 JavaScript 中使用这种类型的值将小数位限制为 4? e 是指数,因为我使用的是十个值的幂。 toFixed()
似乎不起作用。
1.0531436913408342e-7
-5.265718456704172e-7
8.425149530726674e7
How to limit the decimal place to 4 in javascript with this type of values? the e is the exponent since I am using power of ten values. toFixed()
doesn't seem to work.
1.0531436913408342e-7
-5.265718456704172e-7
8.425149530726674e7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
Try:
使用武力,Luke...请参阅
numObj.toExponential(n)
MDN 文档中介绍了一个专门用于限制 以指数表示法显示的小数位:
*numObj*.toExponential([*fractionDigits*])
使用您得到的示例数字:
1.0531436913408342e-7.toExponential(4) // 返回 1.0531e-7
-5.265718456704172e-7.toExponential(4) // 返回 -5.2657e-7
8.425149530726674e7.toExponential(4) // 返回 8.4251e+7
来自 MDN 的引用:
非常方便!
use the force, Luke... see
numObj.toExponential(n)
MDN documents a javascript method specifically for limiting the number of decimal places to show in exponential notation:
*numObj*.toExponential([*fractionDigits*])
Using your example numbers you get:
1.0531436913408342e-7.toExponential(4) // returns 1.0531e-7
-5.265718456704172e-7.toExponential(4) // returns -5.2657e-7
8.425149530726674e7.toExponential(4) // returns 8.4251e+7
Quote from MDN:
Pretty handy!
试试这个方法:
value.toPrecision(1 + 4)
表示 4 位十进制数字。Try this method:
value.toPrecision(1 + 4)
for 4 decimal digits.您可以使用
Math.round();
使用
Math.round(x)
将向上或向下舍入到最接近的整数(使用经典的 0.5 规则)。您可以传入自己的标准并让它保留小数位。下面的链接提供了 1、2 和 3 位小数的示例。
这是链接
You can use
Math.round();
Using
Math.round(x)
will round up or down to the nearest integer (using the classic .5 rule).You can pass in your own criteria and have it do decimal places. The link below has examples for 1, 2, and 3 decimal places.
Here is the link