JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

parseFloat() Function

Name

parseFloat() Function---convert a string to a number

Availability

Navigator 3.0, partial support in Navigator 2.0 and Internet Explorer 3.0

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. Because Navigator 2.0 and Internet Explorer 3.0 do not support NaN, those browsers return 0 when s cannot be parsed.

Description

parseFloat() is a built-in function in JavaScript; it is a core part of the language, and is not a method of any object.

parseFloat() parses and returns the first number that occurs in s. Parsing stops, and the value returned, when parseFloat() encounters a character in s that is not a valid part of the number (i.e., a sign, a digit, decimal point, exponent, etc.) If s does not begin with a number that parseFloat() can parse, then the function returns NaN, a reserved value that represents "not-a-number". You can test for the NaN value with the isNaN() function. If NaN is used with arithmetic operations, the result will always be NaN. In Navigator 2.0 (except Unix platforms) and Internet Explorer 3.0, parseFloat() returns 0 instead of NaN when the input cannot be parsed.

Bugs

In Navigator 2.0 (except Unix platforms) and Internet Explorer 3.0, the NaN return value is not implemented, and so parseFloat() returns 0 when it cannot parse a number. When you receive this return value on those platforms, you must perform additional tests to determine whether the string contains illegal input or actually contains the value 0.

See Also

"isNaN()", "parseInt()"


Previous Home Next
parent Book Index parseInt()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
This HTML Help has been published using the chm2web software.