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


当前回答

其他人的答案并不能给你确切的数字! 这个函数精确地计算所需的数字,并在字符串中返回它,以防止它被javascript更改! 如果你需要一个数值结果,只要乘以第一个函数的结果!

function toNonExponential(value) {
    // if value is not a number try to convert it to number
    if (typeof value !== "number") {
        value = parseFloat(value);

        // after convert, if value is not a number return empty string
        if (isNaN(value)) {
            return "";
        }
    }

    var sign;
    var e;

    // if value is negative, save "-" in sign variable and calculate the absolute value
    if (value < 0) {
        sign = "-";
        value = Math.abs(value);
    }
    else {
        sign = "";
    }

    // if value is between 0 and 1
    if (value < 1.0) {
        // get e value
        e = parseInt(value.toString().split('e-')[1]);

        // if value is exponential convert it to non exponential
        if (e) {
            value *= Math.pow(10, e - 1);
            value = '0.' + (new Array(e)).join('0') + value.toString().substring(2);
        }
    }
    else {
        // get e value
        e = parseInt(value.toString().split('e+')[1]);

        // if value is exponential convert it to non exponential
        if (e) {
            value /= Math.pow(10, e);
            value += (new Array(e + 1)).join('0');
        }
    }

    // if value has negative sign, add to it
    return sign + value;
}

其他回答

您可以循环该数字并实现舍入

//函数替换给定索引的char

String.prototype.replaceAt=function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
}

//开始循环该数字

var str = "123456789123456799.55";
var arr = str.split('.');
str = arr[0];
i = (str.length-1);
if(arr[1].length && Math.round(arr[1]/100)){
  while(i>0){
    var intVal = parseInt(str.charAt(i));

   if(intVal == 9){
      str = str.replaceAt(i,'0');
      console.log(1,str)
   }else{
      str = str.replaceAt(i,(intVal+1).toString()); 
      console.log(2,i,(intVal+1).toString(),str)
      break;
   }
   i--;
 }
}

下面是我的短变体的number .prototype. tofixed方法,适用于任何数字:

Number.prototype.toFixedSpecial = function(n) { var str = this.toFixed(n); if (str.indexOf('e+') === -1) return str; // if number is in scientific notation, pick (b)ase and (p)ower str = str.replace('.', '').split('e+').reduce(function(b, p) { return b + Array(p - b.length + 2).join(0); }); if (n > 0) str += '.' + Array(n + 1).join(0); return str; }; console.log( 1e21.toFixedSpecial(2) ); // "1000000000000000000000.00" console.log( 2.1e24.toFixedSpecial(0) ); // "2100000000000000000000000" console.log( 1234567..toFixedSpecial(1) ); // "1234567.0" console.log( 1234567.89.toFixedSpecial(3) ); // "1234567.890"

你可以用从指数模块。它是轻量级的,并且经过了充分的测试。

import fromExponential from 'from-exponential';

fromExponential(1.123e-10); // => '0.0000000001123'

目前还没有原生的功能来消解科学记数法。但是,出于这个目的,您必须编写自己的功能。

这是我的:

function dissolveExponentialNotation(number)
{
    if(!Number.isFinite(number)) { return undefined; }

    let text = number.toString();
    let items = text.split('e');

    if(items.length == 1) { return text; }

    let significandText = items[0];
    let exponent = parseInt(items[1]);

    let characters = Array.from(significandText);
    let minus = characters[0] == '-';
    if(minus) { characters.splice(0, 1); }
    let indexDot = characters.reduce((accumulator, character, index) =>
    {
        if(!accumulator.found) { if(character == '.') { accumulator.found = true; } else { accumulator.index++; } }
        return accumulator;
    }, { index: 0, found: false }).index;

    characters.splice(indexDot, 1);

    indexDot += exponent;

    if(indexDot >= 0 && indexDot < characters.length - 1)
    {
        characters.splice(indexDot, 0, '.');
    }
    else if(indexDot < 0)
    {
        characters.unshift("0.", "0".repeat(-indexDot));
    }
    else
    {
        characters.push("0".repeat(indexDot - characters.length));
    }

    return (minus ? "-" : "") + characters.join("");
}

我有同样的问题与甲骨文返回科学符号,但我需要一个url的实际数字。我只是用了一个PHP技巧减去0,得到了正确的数字。

例如,5.4987E7是val。

newval = val - 0;

Newval现在等于54987000