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


当前回答

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

// 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为底数。

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

其他回答

下面的解决方案绕过了非常大和非常小的数字的自动指数格式。这是outis的解决方案,有一个错误修正:它不适用于非常小的负数。

function numberToString(num) { let numStr = String(num); if (Math.abs(num) < 1.0) { let e = parseInt(num.toString().split('e-')[1]); if (e) { let negative = num < 0; if (negative) num *= -1 num *= Math.pow(10, e - 1); numStr = '0.' + (new Array(e)).join('0') + num.toString().substring(2); if (negative) numStr = "-" + numStr; } } else { let e = parseInt(num.toString().split('+')[1]); if (e > 20) { e -= 20; num /= Math.pow(10, e); numStr = num.toString() + (new Array(e + 1)).join('0'); } } return numStr; } // testing ... console.log(numberToString(+0.0000000000000000001)); console.log(numberToString(-0.0000000000000000001)); console.log(numberToString(+314564649798762418795)); console.log(numberToString(-314564649798762418795));

你可以使用number.toString(10.1):

console.log(Number.MAX_VALUE.toString(10.1));

注意:这目前适用于Chrome,但不适用于Firefox。规范规定基数必须是整数,所以这会导致不可靠的行为。

这就是我最终使用的从输入中获取值的方法,扩展小于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;
  }
}

我知道这是很多年后的事情了,但我最近一直在研究一个类似的问题,我想把我的解决方案发布出来。目前接受的答案是用0填充指数部分,我试图找到确切的答案,尽管由于JS在浮点精度方面的限制,通常它对于非常大的数字不是完全准确的。

这确实适用于数学。Pow(2,100),返回正确的值1267650600228229401496703205376。

function toFixed(x) { var result = ''; var xStr = x.toString(10); var digitCount = xStr.indexOf('e') === -1 ? xStr.length : (parseInt(xStr.substr(xStr.indexOf('e') + 1)) + 1); for (var i = 1; i <= digitCount; i++) { var mod = (x % Math.pow(10, i)).toString(10); var exponent = (mod.indexOf('e') === -1) ? 0 : parseInt(mod.substr(mod.indexOf('e')+1)); if ((exponent === 0 && mod.length !== i) || (exponent > 0 && exponent !== i-1)) { result = '0' + result; } else { result = mod.charAt(0) + result; } } return result; } console.log(toFixed(Math.pow(2,100))); // 1267650600228229401496703205376

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

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

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

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