parseInt function for string to integer



Enter two numbers

Note: If you enter numbers with decimal values then they will be converted to integer first and then added. Example ( 2.56 + 1.34 = 3 )
parseInt(input_string, radix);
input_string:
Input value as string.
Whitespace before and after are ignored.
If the first character can't be converted to number then NaN is returned.
radix:
The base value of mathematical numeral system. Integer between 2 and 36
<script>
document.write(parseInt(2)); // Output 2
document.write("<br>");
document.write(parseInt(2.4)); // Output 2 
document.write("<br>");
document.write(parseInt(-3.9)); // Output -3 
document.write("<br>");
document.write(parseInt('2.4')); // Output 2
document.write("<br>");
document.write(parseInt('-2.4')); // Output -2
document.write("<br>");
document.write(parseInt('Hello')); //Output  NaN
document.write("<br>");
document.write(parseInt('5ab')) // Output 5
document.write("<br>");
document.write(parseInt('ab5')) // Output NaN 
document.write("<br>");
document.write(parseInt(' 50 ')) // Output 50
document.write("<br>");
document.write(parseInt()); // Output NaN
</script>

What is NaN ?

With radix

With different base numbers.
<script>
document.write(parseInt(15,16)) // Output 21, Hexadecimal Number system
document.write("<br>");
document.write(parseInt(15,8)) // Output 13, Octal Number system
document.write("<br>");
document.write(parseInt(5,2)) // Output NaN, Not a Binary Number system
document.write("<br>");
document.write(parseInt(101,2)) // Output 5, Binary  Number system
document.write("<br>");
</script>
By using parseInt function we can convert strings to integer.

Without Radix and string starting with 0X,0x or 0


Without any value for Radix or if Radix = 0 then if input string starts with 0x or 0X then radix is considered as 16 ( hexadecimal number system )

Similarly if input string starts with 0 then radix is considered as 8 ( Octal number system )

In all other cases ( input string starting with any other value ) Radix is assumed as 10 ( decimal system ).
<script>
document.write(parseInt(0XA)) // Output 10 , Hexadecimal Number system
document.write("<br>");
document.write(parseInt(011)) // Output 9 , Octal Number system
document.write("<br>");
</script>
It is always better to specify Radix ( even for decimal number system )
Here is the code of this demo
<script type="text/javascript"> 
function disp_data(){
document.getElementById("a2").innerHTML=parseInt(document.getElementById('t1').value) + parseInt(document.getElementById('t2').value);
}
</script>

<input type=text name=t1 id='t1' > <input type=text name=t2 id='t2' >
<input type=button value='Display Sum' onClick='disp_data()';> 
<div id='a2'  style=" background-color: #c0f0c0; width:200" > Enter two numbers</div>

Check string if it is a number or not
JavaScript Math Reference
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer