Display of large numbers

C

Cogito

I am using a javascript function to generates large numbers. It
appears that when the number exceeds 20 digits it is displayed in
scientific notation, that is, a base and an exponent. Is there a way
to avoid this and display the complete number instead?
 
T

Toby Inkster

Cogito said:
I am using a javascript function to generates large numbers. It
appears that when the number exceeds 20 digits it is displayed in
scientific notation, that is, a base and an exponent. Is there a way
to avoid this and display the complete number instead?

Hmm... only seems to so this when outputting in base 10.

<script type="text/javascript">
var num = 12;
var pow = 20;
var answer = 1;
for (var i=1; i<=pow; i++)
{
answer = answer * num;
}
document.write('Binary: ');
document.write(answer.toString(2));
document.write('<br>Base Ten: ');
document.write(answer.toString(10));
</script>

You may need to write your own function to re-implement toString. Loop
through the number doing something like this (pseudo-code follows):

while (number > 1)
{
x = number mod 10;
number = floor(number / 10);
retval = x.toString() + retval.toString();
}
return retval;
 
C

cwdjrxyz

Cogito said:
I am using a javascript function to generates large numbers. It
appears that when the number exceeds 20 digits it is displayed in
scientific notation, that is, a base and an exponent. Is there a way
to avoid this and display the complete number instead?

One thing to keep in mind is that JS can handle only 20+ digits, so any
numbers too small or too large to be held within this limit must be
truncated, rounded off, or expressed in scientific notation. Also, be
aware that if you are working with everyday base 10 calculations,
calculations made on digital computers are done in a base 2, or
multiples thereof, number system - binary, octal, hex, etc. If you
calculate using decimal fractions such as in dollars and cents, a
result that should be $2, for example might be given as 1.999999999999
because some numbers used in a calculation can not be represented
exactly in either a base 10 or base 2 number system. You likely can
find more information about this in the FAQs at the Usenet group
comp.lang.javascript, where a link to the FAQs usually is posted every
few days.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top