January 14, 2016
String to Float Conversion in JavaScript
Floats are decimal numbers. Similar to previous tutorial, if a float number is inside a quotation marks, it is treated as string by JavaScript. To convert a string to float, we make use of parseFloat() function. Example is given below.
<html> <head> <title>JavaScript Tutorial </title> </head> <body> <script type="text/javascript"> var f1 = "1.35"; var f2 = "2.55"; var total = f1+f2; var sum = parseFloat(f1) + parseFloat(f2); //Total = 1.352.55 alert(total); //Sum = 3.9 alert(sum); </script> </body> </html>