converter_scientific_notation_to_decimal_notation('1.34E-15')或converter_scientific_notation_to_decimal_notation(1.34E-15)
=> '0.00000000000000134‘
converter_scientific_notation_to_decimal_notation('2.54E-20')或converter_scientific_notation_to_decimal_notation(2.54E-20)
=>‘0.00000000000000000000254’
Javascript中有这样的函数吗?
parseFloat不适用于较大的负科学数。
parseFloat('1.34E-5') => 0.0000134
parseFloat('1.34E-15') => 1.34e-15发布于 2013-04-22 13:12:57
这适用于任何具有指数'E‘(正或负)的正负数。(您可以通过前缀'+‘将数字字符串转换为数字,或使其成为字符串方法,或任何对象的方法,并调用该字符串或数字。)
Number.prototype.noExponents= function(){
var data= String(this).split(/[eE]/);
if(data.length== 1) return data[0];
var z= '', sign= this<0? '-':'',
str= data[0].replace('.', ''),
mag= Number(data[1])+ 1;
if(mag<0){
z= sign + '0.';
while(mag++) z += '0';
return z + str.replace(/^\-/,'');
}
mag -= str.length;
while(mag--) z += '0';
return str + z;
}
var n=2.54E-20;
n.noExponents();返回值:
"0.0000000000000000000254"发布于 2013-04-22 12:36:23
您可以使用toFixed:(1.34E-15).toFixed(18)返回0.000000000000001340
发布于 2018-08-08 06:47:09
您可以使用from-exponential模块。它是轻量级的并且经过了充分的测试。
它接受字符串和数字,因此在转换过程中不会丢失精度。
import fromExponential from 'from-exponential';
fromExponential('1.12345678901234567890e-10');
// '0.00000000011234567890123456789'https://stackoverflow.com/questions/16139452
复制相似问题