SQL PHP HTML ASP JavaScript articles and free scripts to download
 

parseFloat function for string to number

By using parseFloat function we can convert strings to numbers. Using this function we can only convert string numbers to numeric data but alphabetic characters we can't convert.

Main use of the parseFloat function is to convert user entered data of a text box to number as by default all the data entered through a text box is string.

Let us try to learn parseFloat function by using one example. Try to enter two numbers in below given first two text fields. The sum of the two numbers will be displayed inside third text box. Here is the demo.

Enter fist value
Enter Second value
Sum


Here is the code of this demo

<html>

<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function to_add(){
document.f1.t3.value=parseFloat(document.f1.t1.value) + parseFloat(document.f1.t2.value);
}

</script>

</head>

<body>

<form method=post name=f1 ><br>
Enter fist value<input type=text name=t1 onBlur="to_add();"><br>
Enter Second value<input type=text name=t2 onBlur="to_add();"><br>
Sum <input type=text name=t3>
</form>
</body>

</html>


Further readings
random() function to generate random numbers
round() function to get rounded value of a number
IsNaN() function to check data if number or not
ceil() function to get just next higher integer
floor() function to get just highest lower integer
toFixed() function to format decimal places of a number
toPrecision() function to fix the places of a number
parseFloat() function to convert string to number
 
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
JavaScript Tutorials
Math Functions
Popular Tutorials
Drop down list
Timer function
JavaScript Tutorials
String
Array
Date & Time
Form Validation
Event Handling
Math Functions
Loops & structure
JavaScript Forum
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.