Opposite Number.toExponential in JS

I need to get the value of an extremely large number in JavaScript in non-exponential form. Number.toFixed simply returns it exponentially as a string, which is worse than mine.

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" 

I would like to:

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

I could write my own parser, but I would prefer to use my own JS method, if one exists.

+5
source share
5 answers

Answer: there is no such built-in function. I searched high and low. Here I use RegExp to decompose a number into a sign, a coefficient (digits to a decimal point), a fractional part (digits after a decimal point) and an exponent:

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

"Roll your own" is the answer you have already made.

+1
source

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

 x.toPrecision(31) 

However, among the browsers I tested, the above code only works with Firefox. According to the ECMAScript specification, the valid range for toPrecision is 1 to 21, and IE and Chrome respectively throw a RangeError . This is because the floating point representation used in JavaScript is unable to actually represent digits up to 31 digits of precision.

+4
source

"10000000000000000000000000000000"?

It’s hard to believe that someone would rather look at 1.0e + 31,

or in html: 10 31 . But here is one of the methods, most of which relate to negative indicators (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 
+1
source

Use number (string)

Example: var a=Number("1.1e+2"); gives a = 110

+1
source

You can extend the exponential output of JavaScript using string functions. Admittedly, what I came up with is somewhat mysterious, but it works if the index after e 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
source

All Articles