Return to Kroll Design home page

JavaScript Tutorials

 
« Return

parseInt method for finding a number

The parseInt method locates the first number, if any, within the specified string.

For parseInt to find a number, the number must be at the beginning of the string.

If parseInt finds a number at the start of the string, it returns the number.

If parseInt finds no number at the start of the string, it returns "NaN", which means "not a number".

An optional second parameter specifies the radix, or base number system.

If the string starts with a leading zero, parseInt assumes that it's an octal (base 8) number unless you override this by specifying a radix of base 10.

If the string starts with a leading "0x", parseInt assumes that it's a hexidecimal (base 16) number unless you override this by specifying a radix of base 10.

Output:

Code:

<script type="text/javascript">
//<![CDATA[
a = "25xyz"; document.write("1. parseInt(\"", a, "\") is ", parseInt(a), "<br><br>"); b = "oops, 25 is not at the start of the string"; document.write("2. parseInt(\"", b, "\") is ", parseInt(b), "<br><br>"); c = "3abc"; document.write("3. parseInt(\"", c, "\") is ", parseInt(c), "<br><br>"); d = "4 5"; document.write("4. parseInt(\"", d, "\") is ", parseInt(d), "<br><br>"); e = "4.5"; document.write("5. parseInt(\"", e, "\") is ", parseInt(e), "<br><br>"); f = "45"; document.write("6. parseInt(\"", f, "\") is ", parseInt(f), "<br><br>"); g = "$4"; document.write("7. parseInt(\"", g, "\") is ", parseInt(g), "<br><br>"); h = "045"; // leading zero causes javaScript to read it as octal (base 8) document.write("8. parseInt(\"", h, "\") is ", parseInt(h), " : <b>leading zero causes JavaScript to assume that 45 is octal (base 8)</b><br><br>"); document.write("9. parseInt(\"", h, "\", 8) is ", parseInt(h, 8), " : you can specify base 8 even though leading zero made this optional<br><br>"); document.write("10. parseInt(\"", h, "\", 10) is ", parseInt(h, 10), " : specify radix of base 10 to override octal number detection<br><br>"); i = "45"; document.write("11. parseInt(\"", i, "\") is ", parseInt(i), "<br><br>"); document.write("12. parseInt(\"", i, "\" 16 \") is ", parseInt(i, 16), " : specify base 16<br><br>"); j = "0x45"; document.write("13. parseInt(\"", j, "\") is ", parseInt(j), " : leading 0x specifies base 16<br><br>"); document.write("<b>Potential problem if you use parseInt on a string of '08' or '09':</b><br>"); document.write("14. parseInt(\"01\") is ", parseInt("01"), ": 1 is a valid octal number, so parseInt works correctly<br>"); document.write("14. parseInt(\"02\") is ", parseInt("02"), " : 2 is a valid octal number, so parseInt works correctly<br>"); document.write("14. parseInt(\"03\") is ", parseInt("03"), " : 3 is a valid octal number, so parseInt works correctly<br>"); document.write("14. parseInt(\"04\") is ", parseInt("04"), " : 4 is a valid octal number, so parseInt works correctly<br>"); document.write("14. parseInt(\"05\") is ", parseInt("05"), " : 5 is a valid octal number, so parseInt works correctly<br>"); document.write("14. parseInt(\"06\") is ", parseInt("06"), " : 6 is a valid octal number, so parseInt works correctly<br>"); document.write("14. parseInt(\"07\") is ", parseInt("07"), " : 7 is a valid octal number, so parseInt works correctly<br>"); document.write("14. parseInt(\"08\") is ", parseInt("08"), " : 8 is NOT a valid octal number, so parseInt \"bug\" appears<br>"); document.write("14. parseInt(\"09\") is ", parseInt("09"), " : 9 is NOT a valid octal number, so parseInt \"bug\" appears<br><br>"); document.write("work-arounds:<br>"); document.write("14. parseFloat(\"08\") is ", parseFloat("08"), " : use parseFloat instead of parseInt<br>"); document.write("14. parseFloat(\"09\") is ", parseFloat("09"), " : use parseFloat instead of parseInt<br>"); document.write("14. parseInt(\"08\", 10) is ", parseInt("08", 10), " : specify base 10 radix<br>"); document.write("14. parseInt(\"09\", 10) is ", parseInt("09", 10), " : specify base 10 radix<br>"); //]]> </script>


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM