offline version v3


19 of 264 menu

parseFloat function

The parseFloat function converts a string to a floating point number. This is necessary for values like '12.5px' - when the first there is a number, and then units of measurement. If you apply the parseFloat function to '12.5px', the result will be the number 12.5 (and it will really be a number, not a string). The conversion will only occur if the integer is at the beginning of the string, otherwise NaN will be displayed. You can set a number system of a number as a second optional parameter, and the function will return the number converted from the specified number system to decimal.

Syntax

parseFloat(string, [number system]);

Example

Let's extract a fractional number from the beginning of a string:

console.log(parseFloat('10.3px'));

The code execution result:

10.3

Example

In this example, the function will not be able to read the number (because it is not at the beginning of the string) and will return NaN:

let str = 'width: 100px'; console.log(parseFloat(str));

The code execution result:

NaN

See also

  • the parseInt function
    that extracts an integer from the beginning of a string
  • the Number function
    that converts to a number
enru