How can I convert numbers into scientific notation?
Asked Answered
G

3

34

I want to make a function that takes an entered value and converts it to scientific notation (N x 10^a)

I've tried many different things, but I can't seem to get it right.

Example:

I enter 200. The converter converts it to 2 x 10^2

Gawlas answered 20/6, 2012 at 16:56 Comment(1)
Would also like more specific information. i.e how many significant digits is the max? 2,121,212 -> 2 x 10^6 or 2.1 x 10^6 etc etc? What's the "cut off" I guessChaldea
W
74

You can do something like this:

a = 200
a.toExponential(); //output 2e+2

fiddle: http://jsfiddle.net/Q8avJ/9/

Wisconsin answered 20/6, 2012 at 17:1 Comment(3)
Awesome! But is there a way to make it in the form N x 10^a? I can still make do with this, though!Gawlas
@Samar You can use string substitution to replace e with x 10 if you want, even though they mean the same thingWisconsin
toExponential also takes a digits argument like toFixed, to control how many digits the fractional part (before the e)! `Proulx
P
13

At some point I wanted to use the coefficient and the exponent as numbers.

If you want to do that, you can use toExponential function, split the string and convert the items of the array to numbers.

In the following snippet I assign the numbers to the numInSciNot object and print them in the wanted format.

const num = 200;
const numInSciNot = {};
[numInSciNot.coefficient, numInSciNot.exponent] =
  num.toExponential().split('e').map(item => Number(item));
 
console.log(`${numInSciNot.coefficient} x 10^${numInSciNot.exponent}`);

If you don't want to use them as numbers you can just use replace:

const num = 200;
 
console.log(num.toExponential().replace(/e\+?/, ' x 10^'));

In this snippet I've used RegExp to replace e or e+(in the case of positive exponent).

If you want to specify the number of digits after the decimal point, you can use toExponential(NumberOfDigits) in the above examples.

Prismatoid answered 6/11, 2018 at 9:39 Comment(1)
thanks, that's exactly what I wanted. Surprised this isn't part of the standard lib. You can also use point-free style like this: num.toExponential().split('e').map(Number)Verlie
E
2

If you want a base 10 format like this:

m x 10n

Then you can use a function like this:

function writeScientificNum(p_num, p_precision) {
    var n = Math.round(Math.log10(a));
    var m = (p_num * (Math.pow(10,Math.abs(n)))).toFixed(p_precision);
    document.getElementById("outputTxt").innerHTML = m.toString() + ' x 10<sup>' + n.toString() + '</sup>';
}

Test it out: http://jsfiddle.net/u1hd4zm9/

Only odd thing is that the toFixed method (in Chrome at least) will not round exact halves up, but down. For instance if you run this code:

var test = 2.55;
alert(test.toFixed(1));

It will print '2.5' and not '2.6' like you expect. However if you run this code:

var test = 2.5;
alert(test.toFixed(0));

It will print 3. So be aware of that.

Extension answered 23/10, 2017 at 19:4 Comment(1)
The function you list only works for numbers less than 1. If, instead of using (Math.abs(n)) you just use -n, it will work for values >1 too, mapping e.g. 155.0002 to 1.55 x 10^2.Erstwhile

© 2022 - 2024 — McMap. All rights reserved.