Eval Function

T

tozeina

Can any one help in this please.
I'm using eval function in JavaScript, But when the eval method return
a big big number the result will be a number with "E"
for example :
Eval(1000000000000000000000) will result 1e+21
I need to get the number as it is . can i ??

Thanks in advance
 
L

Lasse Reichstein Nielsen

tozeina said:
I'm using eval function in JavaScript,

Probably a bad idea.
But when the eval method return
a big big number the result will be a number with "E"
for example :
Eval(1000000000000000000000) will result 1e+21

That would be the default string representation of that number for
your browser. The notation "1e+21" means 1 * 10^21, i.e., 1 with
21 zeroes after it, which is exactly the number you entered.

You should also be aware, that numbers that big can't all be represented
exactly. E.g., (1e+21 == (1e+21 + 1)) gives true, because the 64 bit
floating point numbers used by Javascript does not have the resolution
to represent (1e+21 + 1).
I need to get the number as it is . can i ??

You can compute your own string representation of the number.
Maybe something like:
---
function numberToString(number, base) {
base = base || 10;
var res = [];
while (number > 0) {
var digit = number % base;
res.push((digit < 10) ? digit : String.fromCharCode(digit - 10 + 97));
number = (number - digit) / base;
}
return res.reverse().join("");
}
---
Call it as:
numberToString(1e+21, 10)
Make sure base is an integer between 2 and 36!

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 10 Aug 2005 05:45:37, seen in
tozeina said:
Can any one help in this please.
I'm using eval function in JavaScript, But when the eval method return
a big big number the result will be a number with "E"
for example :
Eval(1000000000000000000000) will result 1e+21
I need to get the number as it is . can i ??

No doubt you should not in fact be using eval - see newsgroup FAQ 4.40.

In javascript, Numbers are stored as IEEE Doubles, a binary floating-
point format. If you want to see that, which you don't, consider
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#IEEE>.

If you really do need to express a large number in fixed-point, you will
need to accept rounding errors (though it may be possible to mask the
obvious ones) - see BigStrOfMN in
<URL:http://www.merlyn.demon.co.uk/js-round.htm#CAI>.

Alternatively, if you want results accurate past one part in about 1e15,
you can implement your own arithmetic using arrays of digits to
represent numbers. I've done it, for Pascal/Delphi integer work, in
<URL:http://www.merlyn.demon.co.uk/programs/longcalc.pas>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top