javascript 如何将数字转换为科学记数法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11124451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:08:27  来源:igfitidea点击:

How can I convert numbers into scientific notation?

javascripthtmlmathscientific-notation

提问by Samar

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

我想制作一个接受输入值并将其转换为科学记数法 (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

我输入 200。转换器将其转换为 2 x 10^2

回答by Hunter McMillen

You can do something like this:

你可以这样做:

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

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

小提琴:http: //jsfiddle.net/Q8avJ/9/

回答by Manolis Kalafatis

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

在某些时候,我想使用系数和指数作为数字。

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

如果你想这样做,你可以使用toExponential函数,拆分字符串并将数组的项转换为数字。

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

在以下代码段中,我将数字分配给numInSciNot对象并以所需格式打印它们。

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:

如果您不想将它们用作数字,则可以使用replace

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

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

在这个片段中,我使用 RegExp 来替换eor e+(在正指数的情况下)。

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

如果要指定小数点后的位数,可以toExponential(NumberOfDigits)在上面的例子中使用。

回答by Ian

If you want a base 10 format like this:

如果你想要一个像这样的 base 10 格式:

mx 10n

×10 ^ Ñ

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/

测试一下: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:

唯一奇怪的是 toFixed 方法(至少在 Chrome 中)不会将精确的一半向上取整,而是向下取整。例如,如果您运行此代码:

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:

它会像您期望的那样打印“2.5”而不是“2.6”。但是,如果您运行此代码:

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

It will print 3. So be aware of that.

它将打印 3。所以要注意这一点。