Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

parseFloat

Reads a floating-point number from the start of a string.

Available since: ES1

Syntax

parseFloat(string)

Parameters

ParameterTypeDescription
stringstringA string that starts with a number.

Return value

numberA floating-point number, or NaN if the string does not start with a number.

Examples

A number with units:

console.log(parseFloat('3.75kg'));
console.log(parseFloat('12px'));

Output:

3.75
12

When parsing fails:

console.log(parseFloat('kg 3.75'));
console.log(parseFloat('3,75'));

Output:

NaN
3

Notes

The decimal separator must be a dot: a comma ends the number. If the string must contain nothing but a number, Number() is stricter: Number('12px') gives NaN.

See also

  • parseInt — Converts a string to an integer.
  • Number() — Converts a value to a number.
  • Number.isNaN — Checks that a value is NaN, without type conversion.

Related recipes

In the tutorial

  • Type conversion — How to turn a string into a number and back in JavaScript — Number, String, Boolean, parseInt and implicit conversion.

Thanks for reading!

Donate to the site — we work hard for you.