JavaScript parseFloat() function

INTERACTIVE CORNER

<< >>
cash Items  
Subtotal  

Use the arrow keys to scroll through the virtual menu and click on the items to see parseFloat() calculations in action.

JavaScript employs several built in functions to deal with the conversion of strings to numbers. This tutorial will demonstrate a simple example of the JavaScript parseFloat function.

How JavaScript can process strings from various sources

Imagine you had to write a script which needs to handle real numbers.

If those numbers were a user input, or come from more advanced sources such as a database or as values from another website, JavaScript will treat the information as a string.

How parseFloat() works

The JavaScript parseFloat() function parses a string and returns a floating point number (i.e. any real number that has a decimal place or fractional part). This allows the value to be used in calculations and avoids concantenation when numbers are interpreted as strings.

Example


  1. x=prompt("enter number. Only the first number in the string is returned.");
  2. x=parseFloat(b);
  3. alert(x);