limit the exponential notation decimal place to 4 in javascript

佐手、 提交于 2019-12-01 18:28:00

Try:

   Math.round(val*10000)/10000
Jiri Kriz

Try this method: value.toPrecision(1 + 4) for 4 decimal digits.

There's actually 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:

Returns a string representing a Number object in exponential notation with one digit before the decimal point, rounded to fractionDigits digits after the decimal point

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!