How to avoid scientific notation for large numbers in JavaScript?

后端 未结 22 2032
無奈伤痛
無奈伤痛 2020-11-22 00:31

JavaScript converts a large INT to scientific notation when the number becomes large. How can I prevent this from happening?

相关标签:
22条回答
  • 2020-11-22 00:56

    Currently there is no native function to dissolve scientific notation. However, for this purpose you must write your own functionality.

    Here is my:

    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("");
    }
    
    0 讨论(0)
  • 2020-11-22 00:57

    I think there may be several similar answers, but here's a thing I came up with

    // 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);
    

    Note: this is only as accurate as the javascript math functions and has problems when using log instead of log10 on the line before the for loop; it will write 1000 in base-10 as 000 so I changed it to log10 because people will mostly be using base-10 anyways.

    This may not be a very accurate solution but I'm proud to say it can successfully translate numbers across bases and comes with an option for commas!

    0 讨论(0)
  • 2020-11-22 00:59

    I tried working with the string form rather than the number and this seemed to work. I have only tested this on Chrome but it should be universal:

    function removeExponent(s) {
        var ie = s.indexOf('e');
        if (ie != -1) {
            if (s.charAt(ie + 1) == '-') {
                // negative exponent, prepend with .0s
                var n = s.substr(ie + 2).match(/[0-9]+/);
                s = s.substr(2, ie - 2); // remove the leading '0.' and exponent chars
                for (var i = 0; i < n; i++) {
                    s = '0' + s;
                }
                s = '.' + s;
            } else {
                // positive exponent, postpend with 0s
                var n = s.substr(ie + 1).match(/[0-9]+/);
                s = s.substr(0, ie); // strip off exponent chars            
                for (var i = 0; i < n; i++) {
                    s += '0';
                }       
            }
        }
        return s;
    }
    
    0 讨论(0)
  • 2020-11-22 01:01

    You can use number.toString(10.1):

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

    Note: This currently works in Chrome, but not in Firefox. The specification says the radix has to be an integer, so this results in unreliable behavior.

    0 讨论(0)
  • 2020-11-22 01:02

    For small number, and you know how many decimals you want, you can use toFixed and then use a regexp to remove the trailing zeros.

    Number(1e-7).toFixed(8).replace(/\.?0+$/,"") //0.000
    
    0 讨论(0)
  • 2020-11-22 01:02

    Busting out the regular expressions. This has no precision issues and is not a lot of code.

    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'));

    0 讨论(0)
提交回复
热议问题