Script for converting to e+ in javascript

114 Views Asked by At
function eFinder(number) {
    let c = Math.round(number);
    let a = c.toString().length - 1;
    let b = number.toString().charAt(0);
    return `${b}e+${a}`;
}

eFinder(100); would return 1e+2. This basically converts any number put in eFinder(); to scientific notation.

1

There are 1 best solutions below

0
On

Why do you want to implement it by yourself while this is part of JavaScript? Just use Number.prototype.toExponential()

const number = 100;
console.log(number.toExponential());