Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Subject
- Posted on
- Display of large numbers
- 02-01-2006
Re: Display of large numbers
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;
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Re: Display of large numbers
Cogito wrote:
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
- » OT: Php hash
- — Next thread in » HTML Markup Language
- » Xtreme newbie: sending xml requests to server
- — Previous thread in » HTML Markup Language
- » Big data Tutorials
- — Newest thread in » HTML Markup Language
- » archiving question
- — Last Updated thread in » HTML Markup Language
- » SSD partition alignment revisited
- — The site's Newest Thread. Posted in » Computer Hardware