Name
parseFloat() — convert a string to a number
Synopsis
parseFloat
(
s
)
Arguments
s
The string to be parsed and converted to a number.
Returns
The parsed number, or NaN
if s
does not begin with a valid
number. In JavaScript 1.0, parseFloat()
returns 0 instead of
NaN
when
s
cannot be parsed as a number.
Description
parseFloat()
parses and
returns the first number that occurs in
s
. Parsing stops, and the value is
returned, when parseFloat()
encounters a character in s
that is not a
valid part of the number. If s
does not
begin with a number that parseFloat()
can parse, the function
returns the not-a-number value NaN
. Test for this return value with the
isNaN()
function. If you want to
parse only the integer portion of a number, use parseInt()
instead of parseFloat()
.
See Also
Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.