473,594 Members | 2,862 Online

Eval Function

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(1000000000 000000000000) will result 1e+21
I need to get the number as it is . can i ??

Aug 10 '05 #1
4 3497
Generally no.

Javascript numbers have limited precision (i.e. they can only contain a
finite number of digits), following the IEEE standards. See:-

http://en.wikipedia.org/wiki/IEEE_fl...point_standard

You will need to research arbitrary precision mathematics if you really
need greater precision.

Aug 10 '05 #2
"tozeina" <to*****@gmail. com> writes:
I'm using eval function in JavaScript,
<URL:http://jibbering.com/faq/#FAQ4_40>
But when the eval method return
a big big number the result will be a number with "E"
for example :
Eval(1000000000 000000000000) 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.fromChar Code(digit - 10 + 97));
number = (number - digit) / base;
}
return res.reverse().j oin("");
}
---
Call it as:
numberToString( 1e+21, 10)
Make sure base is an integer between 2 and 36!

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 10 '05 #3

The number should come out as is by default, it should't use the Euler's
format, I think the issue is in your using eval(), eval() is outputting
Danny
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Aug 10 '05 #4
JRS: In article <11************ *********@f14g2 000cwb.googlegr oups.com>,
dated Wed, 10 Aug 2005 05:45:37, seen in news:comp.lang. javascript,
tozeina <to*****@gmail. com> posted :
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(100000000 0000000000000) 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.demo n.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.demo n.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.demo n.co.uk/programs/longcalc.pas>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.