当在字符串上下文中使用时,JavaScript将超过21位的整数转换为科学符号。我打印了一个整数作为URL的一部分。我怎样才能阻止这种转变的发生?


当前回答

如果你想将科学计数法转换为整数:

parseInt(“5.6456564564545e+23”, 10)

结果:5

其他回答

这就是我最终使用的从输入中获取值的方法,扩展小于17位的数字并将指数数转换为x10y

// e.g.
//  niceNumber("1.24e+4")   becomes 
// 1.24x10 to the power of 4 [displayed in Superscript]

function niceNumber(num) {
  try{
        var sOut = num.toString();
      if ( sOut.length >=17 || sOut.indexOf("e") > 0){
      sOut=parseFloat(num).toPrecision(5)+"";
      sOut = sOut.replace("e","x10<sup>")+"</sup>";
      }
      return sOut;

  }
  catch ( e) {
      return num;
  }
}

你也可以使用YourJS.fullNumber。例如,YourJS.fullNumber(Number.MAX_VALUE)的结果如下: 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

它也适用于非常小的数字。YourJS.fullNumber(Number.MIN_VALUE)返回: 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005

重要的是要注意,这个函数总是返回有限的数字作为字符串,但会返回非有限的数字(例如。NaN或无穷大)作为未定义的。

你可以在YourJS控制台进行测试。

使用。toprecprecision, . tofixed等。您可以通过将数字转换为. tostring的字符串,然后查看其.length来计算数字中的位数。

我想可能有几个类似的答案,但我想到了一个

// If you're gonna tell me not to use 'with' I understand, just,
// it has no other purpose, ;( andthe code actually looks neater
// 'with' it but I will edit the answer if anyone insists
var commas = false;

function digit(number1, index1, base1) {
    with (Math) {
        return floor(number1/pow(base1, index1))%base1;
    }
}

function digits(number1, base1) {
    with (Math) {
        o = "";
        l = floor(log10(number1)/log10(base1));
        for (var index1 = 0; index1 < l+1; index1++) {
            o = digit(number1, index1, base1) + o;
            if (commas && i%3==2 && i<l) {
                o = "," + o;
            }
        }
        return o;
    }
}

// Test - this is the limit of accurate digits I think
console.log(1234567890123450);

注意:这只与javascript数学函数一样准确,并且在for循环之前的行上使用log而不是log10时存在问题;它会把1000以10为底数写成10000,所以我把它改成了log10,因为大多数人都会用10为底数。

这可能不是一个非常准确的解决方案,但我很自豪地说,它可以成功地跨基数转换数字,并提供了一个逗号选项!

找出正则表达式。这没有精度问题,也不需要很多代码。

function toPlainString(num) { return (''+ +num).replace(/(-?)(\d*)\.?(\d*)e([+-]\d+)/, function(a,b,c,d,e) { return e < 0 ? b + '0.' + Array(1-e-c.length).join(0) + c + d : b + c + d + Array(e-d.length+1).join(0); }); } console.log(toPlainString(12345e+12)); console.log(toPlainString(12345e+24)); console.log(toPlainString(-12345e+24)); console.log(toPlainString(12345e-12)); console.log(toPlainString(123e-12)); console.log(toPlainString(-123e-12)); console.log(toPlainString(-123.45e-56)); console.log(toPlainString('1e-8')); console.log(toPlainString('1.0e-8'));