在JavaScript中解析并将指数值转换为十进制

时间:2013-09-10 13:05:20

标签: javascript

我想使用JavaScript解析并将指数值转换为小数。 4.65661287307739E-10应给出0.000000000465661287307739。我该怎么做才能实现这个目标?

parseFloat(4.65661287307739E-10)返回4.65661287307739e-10。

parseInt(4.65661287307739E-10)返回4.

8 个答案:

答案 0 :(得分:14)

您可以使用toFixed(),但限制为20.

前:

(4.65661287307739E-10).toFixed(20)
"0.00000000046566128731"

但是......

(4.65661287307739E-30).toFixed(20)
"0.00000000000000000000"

所以,如果你总是少于20个小数位,你会没事的。否则,我想你可能要写自己的。

答案 1 :(得分:14)

您可以显示大小数字的字符串值:

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=4.65661287307739E-10 ;
n.noExponents()

/*  returned value: (String)
0.000000000465661287307739
*/

答案 2 :(得分:4)

用ES6编写的可怕的原始转换:

function convert(n){
    var [lead,decimal,pow] = n.toString().split(/e|\./);
    return +pow <= 0 
        ? "0." + "0".repeat(Math.abs(pow)-1) + lead + decimal
        : lead + ( +pow >= decimal.length ? (decimal + "0".repeat(+pow-decimal.length)) : (decimal.slice(0,+pow)+"."+decimal.slice(+pow)))
}

var myvar = 4.951760157141521e+27;
var myvar2 = 4.951760157141521e-2;
convert(myvar);//"4951760157141521000000000000"
convert(myvar2);//"0.04951760157141521"

答案 3 :(得分:1)

扩大了@kennebec的答案,但处理了他的失败的边缘情况(Gist, with CoffeeScript):

&#13;
&#13;
  String.prototype.noExponents = function(explicitNum) {
    var data, leader, mag, multiplier, num, sign, str, z;
    if (explicitNum == null) {
      explicitNum = true;
    }

    /*
     * Remove scientific notation from a number
     *
     * After
     * http://stackoverflow.com/a/18719988/1877527
     */
    data = this.split(/[eE]/);
    if (data.length === 1) {
      return data[0];
    }
    z = "";
    sign = this.slice(0, 1) === "-" ? "-" : "";
    str = data[0].replace(".", "");
    mag = Number(data[1]) + 1;
    if (mag <= 0) {
      z = sign + "0.";
      while (!(mag >= 0)) {
        z += "0";
        ++mag;
      }
      num = z + str.replace(/^\-/, "");
      if (explicitNum) {
        return parseFloat(num);
      } else {
        return num;
      }
    }
    if (str.length <= mag) {
      mag -= str.length;
      while (!(mag <= 0)) {
        z += 0;
        --mag;
      }
      num = str + z;
      if (explicitNum) {
        return parseFloat(num);
      } else {
        return num;
      }
    } else {
      leader = parseFloat(data[0]);
      multiplier = Math.pow(10, parseInt(data[1]));
      return leader * multiplier;
    }
  };

  Number.prototype.noExponents = function() {
    var strVal;
    strVal = String(this);
    return strVal.noExponents(true);
  };
&#13;
&#13;
&#13;

答案 4 :(得分:1)

对于我来说,此变体非常适合显示太小/太大的格式化字符串值:

const exponentialToDecimal = exponential => {
    let decimal = exponential.toString().toLowerCase();
    if (decimal.includes('e+')) {
        const exponentialSplitted = decimal.split('e+');
        let postfix = '';
        for (
            let i = 0;
            i <
            +exponentialSplitted[1] -
                (exponentialSplitted[0].includes('.') ? exponentialSplitted[0].split('.')[1].length : 0);
            i++
        ) {
            postfix += '0';
        }
        const addCommas = text => {
            let j = 3;
            let textLength = text.length;
            while (j < textLength) {
                text = `${text.slice(0, textLength - j)},${text.slice(textLength - j, textLength)}`;
                textLength++;
                j += 3 + 1;
            }
            return text;
        };
        decimal = addCommas(exponentialSplitted[0].replace('.', '') + postfix);
    }
    if (decimal.toLowerCase().includes('e-')) {
        const exponentialSplitted = decimal.split('e-');
        let prefix = '0.';
        for (let i = 0; i < +exponentialSplitted[1] - 1; i++) {
            prefix += '0';
        }
        decimal = prefix + exponentialSplitted[0].replace('.', '');
    }
    return decimal;
};

const result1 = exponentialToDecimal(5.6565e29); // "565,650,000,000,000,000,000,000,000,000"
const result2 = exponentialToDecimal(5.6565e-29); // "0.000000000000000000000000000056565"

答案 5 :(得分:0)

这是我的简短功能代码,它将指数(电子符号)数字转换为小数, 允许输出大量小数位

它处理Javascript允许的所有电子注释语法,包括以下内容:

Valid e-notation numbers in Javascript:
   123e1    ==>  1230
   123E1    ==>  1230
   123e+1   ==>  1230
   123.e+1  ==>  1230   (with missing fractional part)
   123e-1   ==>  12.3
   0.1e-1   ==>  0.01
   .1e-1    ==>  0.01   (with missing whole part)
  -123e1    ==> -1230
   0.0e1    ==>  0
  -0.0e4    ==> -0

该函数不会尝试捕获NaN或未定义的输入,但会尝试满足常规(非e表示法)数字。这样的数字按“原样”返回。

我已尝试在每行上提供足够的注释,并试图(尽可能避免!)使用短路和条件(三元)运算符,以提高清晰度。

我已经使用toLocaleString()方法来自动检测小数点分隔符,但这当然假定代表数字的输入字符串遵循机器的语言环境(尤其是在手动传递给函数时)。 / p>

/********************************************************
* Converts Exponential (e-Notation) Numbers to Decimals
********************************************************
* @function numberExponentToLarge()
* @version  1.00
* @param   {string}  Number in exponent format.
*                   (other formats returned as is).
* @return  {string}  Returns a decimal number string.
* @author  Mohsen Alyafei
* @date    12 Jan 2020
*
* Notes: No check is made for NaN or undefined inputs
*
*******************************************************/

function numberExponentToLarge(numIn) {
 numIn +="";                                            // To cater to numric entries
 var sign="";                                           // To remember the number sign
 numIn.charAt(0)=="-" && (numIn =numIn.substring(1),sign ="-"); // remove - sign & remember it
 var str = numIn.split(/[eE]/g);                        // Split numberic string at e or E
 if (str.length<2) return sign+numIn;                   // Not an Exponent Number? Exit with orginal Num back
 var power = str[1];                                    // Get Exponent (Power) (could be + or -)

 var deciSp = 1.1.toLocaleString().substring(1,2);  // Get Deciaml Separator
 str = str[0].split(deciSp);                        // Split the Base Number into LH and RH at the decimal point
 var baseRH = str[1] || "",                         // RH Base part. Make sure we have a RH fraction else ""
     baseLH = str[0];                               // LH base part.

  if (power>=0) {   // ------- Positive Exponents (Process the RH Base Part)
     if (power> baseRH.length) baseRH +="0".repeat(power-baseRH.length); // Pad with "0" at RH
     baseRH = baseRH.slice(0,power) + deciSp + baseRH.slice(power);      // Insert decSep at the correct place into RH base
      if (baseRH.charAt(baseRH.length-1) ==deciSp) baseRH =baseRH.slice(0,-1); // If decSep at RH end? => remove it

  } else {         // ------- Negative exponents (Process the LH Base Part)
     num= Math.abs(power) - baseLH.length;                               // Delta necessary 0's
     if (num>0) baseLH = "0".repeat(num) + baseLH;                       // Pad with "0" at LH
     baseLH = baseLH.slice(0, power) + deciSp + baseLH.slice(power);     // Insert "." at the correct place into LH base
     if (baseLH.charAt(0) == deciSp) baseLH="0" + baseLH;                // If decSep at LH most? => add "0"

  }
  // Rremove leading and trailing 0's and Return the long number (with sign)
  return sign + (baseLH + baseRH).replace(/^0*(\d+|\d+\.\d+?)\.?0*$/,"$1");
}

//============ test codes ==================
function test(test,input,should){
var  out=numberExponentToLarge(input);
var r = (out===should) ? true : false;
if (!r) console.log(test+" Failed: "+out+" should be: "+should);
  else console.log("Passed");
}
// ------------- tests for e-notation numbers ---------------------

test(1,"123E0","123")
test(2,"123E0","123")
test(3,"-123e+0","-123")
test(4,"123e1","1230")
test(5,"123e3","123000")
test(6,"123e+3","123000")
test(7,"123E+7","1230000000")
test(8,"-123.456e+1","-1234.56")
test(9,"123.456e+4","1234560")
test(10,"123E-0","123")
test(11,"123.456e+50","12345600000000000000000000000000000000000000000000000")

test(12,"123e-0","123")
test(13,"123e-1","12.3")
test(14,"123e-3","0.123")
test(15,"-123e-7","-0.0000123")
test(16,"123.456E-1","12.3456")
test(17,"123.456e-4","0.0123456")
test(18,"123.456e-50","0.00000000000000000000000000000000000000000000000123456")
test(18-1,"-123.456e-50","-0.00000000000000000000000000000000000000000000000123456")

test(19,"1.e-5","0.00001")  // handle missing base fractional part
test(20,".123e3","123")     // handle missing base whole part

// The Electron's Mass: 
test(21,"9.10938356e-31","0.000000000000000000000000000000910938356")
// The Earth's Mass:
test(22,"5.9724e+24","5972400000000000000000000")
// Planck constant:
test(23,"6.62607015e-34","0.000000000000000000000000000000000662607015")

test(24,"0.000e3","0")
test(25,"0.000000000000000e3","0")
test(26,"-0.0001e+9","-100000")
test(27,"-0.0e1","-0")
test(28,"-0.0000e1","-0")
test(28,"-000.0000e1","-0")    // this is an invalid Javascript number
test(28,"-000.0000e-1","-0")   // this is an invalid Javascript number
test(28,"-000.0000e+10","-0")  // this is an invalid Javascript number
test(28,"-000.0000e+2","-0")   // this is an invalid Javascript number

// ------------- testing for Non e-Notation Numbers -------------
test(29,"12345.7898","12345.7898") // no exponent
test(30,12345.7898,"12345.7898")   // no exponent
test(31,0.00000000000001,"0.00000000000001")    // from 1e-14
test(32,-0.0000000000000345,"-0.0000000000000345") // from -3.45e-14
test(33,-0,"0")
test(34,"1.2000e0","1.2")
test(35,"1.2000e-0","1.2")
test(35,"1.2000e+0","1.2")
test(35,"1.2000e+10","12000000000")

答案 6 :(得分:0)

使用固定(数字)

例如:

 - index.ejs   
   - mainMenu.ejs
   - systemsTable.ejs
     - systemRow.ejs
     - systemStatusIcon.ejs
     - systemName.ejs
     - ....

答案 7 :(得分:-3)

在ExtJS中,您可以使用内部使用toFixed()方法的Ext.Number.toFixed(value, precision)方法,

E.g。

console.log(Ext.Number.toFixed(4.65661287307739E-10, 10));  
// O/p => 0.0000000005

console.log(Ext.Number.toFixed(4.65661287307739E-10, 15));  
// 0.000000000465661