1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Number Bases and Leading Zeros

Join the Discussion

Questions? Comments?

One thing that can cause problems when writing Javascript is where have a date that has been entered and you are splitting it up to convert the separate day and month portions into numbers to use for subsequent processing. Where these numbers are always entered as two digits the '08' and '09' values often do not get converted into numbers in the way that you would expect. Using parseInt('09') givers you zero as the number that is returned rather than the 9 that you expect.

Everyone is familiar with the base 10 (or decimal) way of counting where we have ten separate symbols to represent different values - 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. To get values greater than 9 (the largest number having a separate symbol) we start combining our symbols together and use 10 to represent the number one bigger than 9.

At this point you are probably wondering what the above two paragraphs have in common and why I have explained something so obvious as our numbering system the way that I have. The answer is that the decimal numbering system is not the only possible numbering system and is in fact not the one that computers themselves use.

While we prefer to count using the decimal system, computers prefer to count using the binary (base 2) system. Binary has only two sysmbols to represent different values - 0 and 1. To get values greater than 1 we combine sysbols together the same way that we do in our decimal system and so in binary 10 represents the number one bigger than 1 (which is represented with 2 in our decimal system). Even 1001 is not a very big number in binary as it can still be represented with just one symbol in decimal since it is the binary equivalent of 9.

The biggest problem with binary numbers is that we can end up with large combinations of 1s and 0s that can take us a long time to work out what the decimal equivalent is so that we will know what the number really represents. To help us to solve this problem we introduced two further number bases that computers can easily convert to from binary that are also easier for us to work with. These two number bases are octal (base 8) which uses 0, 1, 2, 3, 4, 5, 6, and 7 and hexadecimal (base 16) which uses 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F (allowing numbers up to 15 to be represented by a single symbol). Computers can easily convert binary to one of these because it is simply a matter of taking three or four binary digits (usually abbreviated to bits) and representing them as the one equivalent symbol in the octal or hexadecimal system. Since octal and hexadecimal numbers are approximately the same length as the equivalent decimal numbers they are much easier for us to work with than binary.

Javascript (like most programming languages) allows us to work directly with both octal and hexadecimal numbers, all we need is a way to tell which number base we are using when we specify a number. To identify octal and hexadecimal numbers we add something to the front of numbers using those bases to indicate which base we are using. A leading 0 on the front of a number indicates that the number following is octal while a leading 0x indicates a hexadecimal number. The decimal number 18 can therefore also be represented as 022 (in octal) and 0x12 (in hexadecimal). We don't put a special symbol on the front of decimal numbers so any number that doesn't start with 0 or 0x is assumed to be decimal.

When we use parseInt() we can specify the number base that we want the resulting number to be in by specifying the number base in the second (optional) parameter. If we don't specify a number base in that parameter then the number base will be worked out from the content of the number itself. With parseint('09') we are asking the function to extract the number from this string in octal. Since 9 is not a number in the octal numbering system the function drops it and returns the number portion preceding it giving us 0. Of course the function treats 01 through 07 as octal numbers as well but since these octal numbers are identical to their decimal equivalents we don't notice when these numbers are returned that the function is using the wrong number base. To force the parseInt function to extract decimal numbers from the string all we need to do is to pass 10 in the second parameter. The following will return the 9 that we expect./p>

var num = parseInt('09',10);

Note that the ECMAscript standards do not require browsers to support octal numbers in Javascript and so you should probably avoid using them just in case there are any browsers out there that don't support it. Since you probably had no idea of what octal numbers were before reading this article this should not be a problem.

See More About

©2017 About.com. All rights reserved.