A decimal value is an integer composed of only the following characters:
0 1 2 3 4 5 6 7 8 9
Decimal values are never prefixed by a zero character.
The sequence carries over for the next increment when each column reaches the value 9. Thus:
0 1 2 3 4 5 6 7 8 9 10 11 12
Converting between decimal, hexadecimal, and binary values is quite complicated. Here are the first 16 values in all number bases:
Beware when you prefix decimal values with a zero character. You may want to justify a column of figures. If you add leading zero characters to a numeric string, if that string is subsequently parsed back to a numeric value, you may inadvertently export the value as a decimal but import it as an octal value. This can lead to an extremely difficult to diagnose fault in your software because the parsers sometimes add some intelligence and will correctly interpret the value as decimal if the characters 8 or 9 are present but otherwise interpret it as octal notation.
Be careful that you remove any leading zero characters from the text strings that you plan to convert using the numeric parser.
This may be implementation dependent behavior to some extent.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> // Create a 0-255 lookup table document.write("<TABLE BORDER=1>"); document.write("<TR><TH>Index</TH>"); document.write("<TH>Binary</TH>"); document.write("<TH>Octal</TH>"); document.write("<TH>Hex</TH>"); document.write("<TH>Char</TH></TR>"); for(ii=0; ii<256; ii++) { myBinary = ii.toString(2); myBinaryPadding = "00000000".substr(1,(8-myBinary.length)); myOctal = ii.toString(8); myOctalPadding = "0000".substr(1,(4-myOctal.length)); myHex = ii.toString(16); myHexPadding = "00".substr(1,(2-myHex.length)); myChar = String.fromCharCode(ii); document.write("<TR ALIGN=RIGHT><TD>"); document.write(ii); document.write("</TD><TD>"); document.write(myBinaryPadding+myBinary); document.write("</TD><TD>"); document.write(myOctalPadding+myOctal); document.write("</TD><TD>"); document.write(myHexPadding+myHex.toUpperCase()); document.write("</TD><TD>"); document.write(" "+myChar); document.write("</TD></TR>"); } document.write("</TABLE>"); </SCRIPT> </BODY> </HTML>
Prev | Home | Next |
Decimal point (.) | Up | Declaration |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |