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>
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>
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>
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>
<!DOCTYPE html>
<html>
<head>
<title>Number Example</title>
<script type="text/javascript">
document.writeln(parseFloat("1.234e7")); //12340000
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Data Types:
- JavaScript Data Types
- typeof Operator
- The Undefined Type
- null Type
- null vs undefined
- Boolean Type
- Boolean() casting function
- The Literials of Number Type
- Octal Integer
- Hexadecimal
- Floating-Point Values
- Value range
- NaN
- Number Conversions:Number(), parseInt() and parseFloat()
- Number() function
- parseInt()
- parseFloat()
- The String Type
- String Literals and Escapes
- Get the String Length
- Converting to a String with toString() method
- Convert Number to String with radix
- Convert to String with String() casting function