You are probably better off with the exponential notation.
Javascript loses precision after 15 or so digits.
9284476546528123456781 may be rounded to
9284476546528124000000, and
0.00000000009284476546528123456781 to
0.00000000009284476546528124
If you need more precision you can use a javascript big integer library,
or hand off the calculations to a library on the server.
If you just want to replace the notation with the rounded number,
you can do it with a replace operation on the string value of the number.
Numbers smaller than zero will be returned with the zeroes in front of the first significant digit,
big numbers will add zeroes to the end.
- Number.prototype.todigits= function(){
-
var tem='', z, d, s= this.toString(),
-
x= s.match(/^(\d+)\.(\d+)[eE]([-+]?)(\d+)$/);
-
if(x){
-
d= x[2];
-
z= (x[3]== '-')? x[4]-1: x[4]-d.length;
-
while(z--)tem+='0';
-
if(x[3]== '-'){
-
return '0.'+tem+x[1]+d;
-
}
-
return x[1]+d+tem;
-
}
-
return s;
-
}
- //TEST
-
var n= 0.00000000009284476546528123456781,
-
n2= 9284476546528123456781;
-
alert(n+'\n'+n.todigits()+'\n'+ n2+'\n'+n2.todigits())
-
-
returned value: (String):
-
9.284476546528124e-11
-
0.00000000009284476546528124
-
9.284476546528124e+21
-
9284476546528124000000