revision:
The toExponential() method returns a string representing the Number object in exponential notation.
toExponential() toExponential(fractionDigits)
Parameters:
fractionDigits :Optional. An integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number. An integer between 0 and 20 representing the number of digits in the notation after the decimal point. If omitted, it is set to as many digits as necessary to represent the value
function expo(x, f) { return Number.parseFloat(x).toExponential(f); } console.log(expo(123456, 2)); // Expected output: "1.23e+5" console.log(expo('123456')); // Expected output: "1.23456e+5" console.log(expo('oink')); // Expected output: "NaN"
<div> <p id="exponent-1"></p> <p id="exponent-2"></p> <p id="exponent-3"></p> <p id="exponent-4"></p> <p id="exponent-5"></p> <p id="exponent-6"></p> <p id="exponent-7"></p> </div> <script> const numObj = 77.1234; document.getElementById("exponent-1").innerHTML = " number : " + numObj; console.log(numObj.toExponential()); // 7.71234e+1 console.log(numObj.toExponential(4)); // 7.7123e+1 console.log(numObj.toExponential(2)); // 7.71e+1 console.log((77.1234).toExponential()); // 7.71234e+1 console.log((77).toExponential()); // 7.7e+1 document.getElementById("exponent-2").innerHTML = " number toExponential(): " + numObj.toExponential(); document.getElementById("exponent-3").innerHTML = " number toExponential(4): " + numObj.toExponential(4); document.getElementById("exponent-4").innerHTML = " number toExponential(2): " + numObj.toExponential(2); document.getElementById("exponent-5").innerHTML = " number toExponential(): " + (77.1234).toExponential(); document.getElementById("exponent-6").innerHTML = " number toExponential(): " + (77).toExponential();