26 May 2012

How to Convert HTML Text to Number in Javascript

javascript
Using a function , a string can be converted to integer in Javascript.
In Javascript, functions that are used to convert from text to number are :
parseInt() , parseFloat(), number()

parseInt() fuction converts text to round number format in javascript.

Example:
var num1 = document.getElementById("textbox1").value;
var num2 = document.getElementById("textbox2").value;
var ans = parseInt( num1 + num2 );

parseFloat() function coverts text to floating number in javascript.

Example:
var num1 = document.getElementById("textbox1").value;
var num2 = document.getElementById("textbox2").value;
var ans = parseFloat( num1)  +parseFloat(num2 );

::View CHEAT SHEET of JAVASCRIPT ::

(If you found this article useful then share with your friends.)

No comments: