Opposite of Number.toExponential in JS

前端 未结 5 597
感情败类
感情败类 2020-12-10 16:36

I need to get the value of an extremely large number in JavaScript in non-exponential form. Number.toFixed simply returns it in exponential form as a string, wh

相关标签:
5条回答
  • 2020-12-10 16:46

    The answer is there's no such built-in function. I've searched high and low. Here's the RegExp I use to split the number into sign, coefficient (digits before decimal point), fractional part (digits after decimal point) and exponent:

    /^([+-])?(\d+)\.?(\d*)[eE]([+-]?\d+)$/
    

    "Roll your own" is the answer, which you already did.

    0 讨论(0)
  • 2020-12-10 16:47

    It's possible to expand JavaScript's exponential output using string functions. Admittedly, what I came up is somewhat cryptic, but it works if the exponent after the e is positive:

    var originalNumber = 1e+31;
    var splitNumber = originalNumber.toString().split('e');
    
    var result;
    if(splitNumber[1]) {
        var regexMatch = splitNumber[0].match(/^([^.]+)\.?(.*)$/);
        result =
            /* integer part */ regexMatch[1] +
            /* fractional part */ regexMatch[2] +
            /* trailing zeros */ Array(splitNumber[1] - regexMatch[2].length + 1).join('0');
    } else result = splitNumber[0];
    
    0 讨论(0)
  • 2020-12-10 16:59

    "10000000000000000000000000000000"?

    Hard to believe that anybody would rather look at that than 1.0e+31,

    or in html: 1031. But here's one way, much of it is for negative exponents(fractions):

    function longnumberstring(n){
        var str, str2= '', data= n.toExponential().replace('.','').split(/e/i);
        str= data[0], mag= Number(data[1]);
        if(mag>=0 && str.length> mag){
            mag+=1;
            return str.substring(0, mag)+'.'+str.substring(mag);            
        }
        if(mag<0){
            while(++mag) str2+= '0';
            return '0.'+str2+str;
        }
        mag= (mag-str.length)+1;
        while(mag> str2.length){
            str2+= '0';
        }
        return str+str2;
    }
    
    
    
    input: 1e+30
    longnumberstring: 1000000000000000000000000000000
    to Number: 1e+30
    input: 1.456789123456e-30
    longnumberstring: 0.000000000000000000000000000001456789123456
    to Number: 1.456789123456e-30
    input: 1.456789123456e+30
    longnumberstring: 1456789123456000000000000000000
    to Number: 1.456789123456e+30
    input: 1e+80 longnumberstring: 100000000000000000000000000000000000000000000000000000000000000000000000000000000
    to Number: 1e+80
    
    0 讨论(0)
  • 2020-12-10 17:01

    Use Number(string)

    Example :

    var a = Number("1.1e+2");
    

    Return :

    a = 110

    0 讨论(0)
  • 2020-12-10 17:05

    You can use toPrecision with a parameter specifying how many digits you want to display:

    x.toPrecision(31)
    

    However, among the browsers I tested, the above code only works on Firefox. According to the ECMAScript specification, the valid range for toPrecision is 1 to 21, and both IE and Chrome throw a RangeError accordingly. This is due to the fact that the floating-point representation used in JavaScript is incapable of actually representing numbers to 31 digits of precision.

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