Display of large numbers

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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?

Re: Display of large numbers

Cogito wrote:

Quoted text here. Click to load it

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('<br>Base Ten: ');

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;

Toby A Inkster BSc (Hons) ARCS
Contact Me  ~ http://tobyinkster.co.uk/contact

Re: Display of large numbers

Cogito wrote:
Quoted text here. Click to load it

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.

Site Timeline