Naši dobrovolníci ještě tento článek do jazyka Čeština nepřeložili. Přidejte se a pomozte nám tuto práci dokončit!
Tento článek si můžete přečíst také v jazyce English (US).

The parseFloat() function parses an argument and returns a floating point number.

Syntax

parseFloat(value)

Parameters

value
The value you want to parse.

Return value

A floating point number parsed from the given value. If the value cannot be converted to a number, NaN is returned.

Description

parseFloat is a top-level function and is not associated with any object.

parseFloat parses its argument, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.

If the value is a string and first character cannot be converted to a number, parseFloat returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

parseFloat can also parse and return the value Infinity. You can use the isFinite function to determine if the result is a finite number (not Infinity, -Infinity, or NaN).

parseFloat is also able to parse an object if it has defined toString or valueOf method. In case the value is number same number is returned.

Examples

parseFloat returning a number

The following examples all return 3.14

parseFloat(3.14);
parseFloat('3.14');
parseFloat('314e-2');
parseFloat('0.0314E+2');
parseFloat('3.14more non-digit characters');

var foo = Object.create(null);
foo.toString = function () { return "3.14"; };
parseFloat(foo);

var foo = Object.create(null);
foo.valueOf = function () { return "3.14"; }; 
parseFloat(foo);​​​​​

 

parseFloat returning NaN

The following example returns NaN

parseFloat('FF2');

A stricter parse function

It is sometime useful to have a stricter way to parse float values, regular expressions can help :

var filterFloat = function(value) {
    if (/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
      .test(value))
      return Number(value);
  return NaN;
}

console.log(filterFloat('421'));               // 421
console.log(filterFloat('-421'));              // -421
console.log(filterFloat('+421'));              // 421
console.log(filterFloat('Infinity'));          // Infinity
console.log(filterFloat('1.61803398875'));     // 1.61803398875
console.log(filterFloat('421e+0'));            // NaN
console.log(filterFloat('421hop'));            // NaN
console.log(filterFloat('hop1.61803398875'));  // NaN

Note that this code is an example only; it does not accept valid numbers such as 1. or .5.

Specifications

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
The definition of 'parseFloat' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'parseFloat' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'parseFloat' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes Yes Yes Yes Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic support Yes Yes Yes Yes Yes Yes Yes

See also

Štítky a přispěvatelé do dokumentace

Štítky: 
 Poslední aktualizace od: fscholz,