parseFloat()

The parseFloat() function looks at each character starting in position 0. It continues to convert until it reaches either the end of the string or an invalid character. A decimal point is valid the first time not the second time.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(parseFloat("12.34.5")); //12.34

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

parseFloat() ignores initial zeros.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(parseFloat("012.34.5")); //12.34

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Hexadecimal numbers become 0.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(parseFloat("0xFF")); //0

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

If the string represents a whole number, parseFloat() returns an integer.

 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(parseFloat("1234asdf")); //1234

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(parseFloat("1.234e7")); //12340000

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Data Types:
  1. JavaScript Data Types
  2. typeof Operator
  3. The Undefined Type
  4. null Type
  5. null vs undefined
  6. Boolean Type
  7. Boolean() casting function
  8. The Literials of Number Type
  9. Octal Integer
  10. Hexadecimal
  11. Floating-Point Values
  12. Value range
  13. NaN
  14. Number Conversions:Number(), parseInt() and parseFloat()
  15. Number() function
  16. parseInt()
  17. parseFloat()
  18. The String Type
  19. String Literals and Escapes
  20. Get the String Length
  21. Converting to a String with toString() method
  22. Convert Number to String with radix
  23. Convert to String with String() casting function