How to use parseInt in JavaScript

This article describe about parseInt() Function in JavaScript.
  • 1445

parseInt() Function in JavaScript

This function parse the string and returns the integer values.

For numeral system to be used radix parameter is used , for example  a radix of 8 (Octal) indicates that the number in the string should be parsed from a Octal number to a decimal number.

Note: This function parse the string and return the integer.

If the radix parameter is omitted, JavaScript assumes the following:

The radix is 16 (hexadecimal) if the string begins with "0x".

The radix is 8 (octal). This feature is deprecated, if the string begins with "0".

The radix is 10 (decimal), if the string begins with any other value.

Syntax

parseInt(string, radix)

 

Parameter Description
string Required. to string for parse.
radix Optional. A number (from 2 to 36) that represents the numeral system to be used

Example

<!DOCTYPE html>

<html>

<body>

<script type="text/javascript">

    document.write(parseInt("15") + "<br />");

    document.write(parseInt("15.55") + "<br />");

    document.write(parseInt("44 54 64") + "<br />");

    document.write(parseInt(" 50 ") + "<br />");

    document.write(parseInt("60 days") + "<br />");

    document.write(parseInt("I am 60 year old") + "<br />");

    document.write("<br />");

    document.write(parseInt("15", 15) + "<br />");

    document.write(parseInt("015") + "<br />");

    document.write(parseInt("15", 13) + "<br />");

    document.write(parseInt("0x15") + "<br />");

    document.write(parseInt("20", 16) + "<br />");

</script>

</body>

</html>

 

Output

 

pic9.jpg 

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.