开发者

Opposite of Number.toExponential in JS

开发者 https://www.devze.com 2023-01-24 16:55 出处:网络
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, which is worse than what I

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, which is worse than what I had.

This is what Number.toFixed returns:

>>> x = 1e+31
1e+31
>>> x.toFixed()
"1e+31"

Number.toPrecision also does not work:

>>> x = 1e+31
1e+31
>>> x.toPrecision( 21 )
"9.99999999999999963590e+30"

What I would like is:

>>> x = 1e+31
1e+31
>>> x.toNotExponential()
"10000000000000000000000000000000"

I could write my own parser but I would rather use a native JS method if one exists.


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.


Use Number(string)

Example :

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

Return :

a = 110


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.


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];


"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

精彩评论

暂无评论...
验证码 换一张
取 消