By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Eval Function

P: n/a
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

Aug 10 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
"tozeina" <to*****@gmail.com> writes:
I'm using eval function in JavaScript,
Probably a bad idea.
<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(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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 10 '05 #3

P: n/a


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
such, instead, skip eval() altogether.
Danny
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Aug 10 '05 #4

P: n/a
JRS: In article <11*********************@f14g2000cwb.googlegroups. 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(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>.

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

This discussion thread is closed

Replies have been disabled for this discussion.