limit the exponential notation decimal place to 4 in javascript

前端 未结 4 1426
深忆病人
深忆病人 2021-01-18 17:36

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

4条回答
  •  自闭症患者
    2021-01-18 17:51

    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:

    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

    Pretty handy!

提交回复
热议问题