parseFloat()

parseFloat() 函数解析字符串参数并返回浮点数。

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

Try it

语法

¥Syntax

js
parseFloat(string)

参数

¥Parameters

string

要解析的值,强制为字符串。该参数中的前导 whitespace 将被忽略。

返回值

¥Return value

当第一个非空白字符无法转换为数字时,从给定的 stringNaN 解析出的浮点数。

¥A floating point number parsed from the given string, or NaN when the first non-whitespace character cannot be converted to a number.

注意:JavaScript 在语言层面上没有 "浮点数字" 和 "integers" 的区别。parseInt()parseFloat() 仅在解析行为上有所不同,但其返回值不一定不同。例如,parseInt("42")parseFloat("42") 将返回相同的值:Number 42。

¥Note: JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level. parseInt() and parseFloat() only differ in their parsing behavior, but not necessarily their return values. For example, parseInt("42") and parseFloat("42") would return the same value: a Number 42.

描述

¥Description

parseFloat 函数将其第一个参数转换为字符串,将该字符串解析为十进制数字文字,然后返回数字或 NaN。它接受的数字语法可以概括为:

¥The parseFloat function converts its first argument to a string, parses that string as a decimal number literal, then returns a number or NaN. The number syntax it accepts can be summarized as:

  • parseFloat() 接受的字符包括加号 (+)、减号 (- U+002D HYPHEN-MINUS)、十进制数字 (09)、小数点 (.)、指数指示符(eE)和 "Infinity" 文字 。
  • +/- 符号只能严格出现在字符串的开头,或者紧跟在 e/E 字符之后。小数点只能出现一次,并且只能出现在 e/E 字符之前。e/E 字符只能出现一次,并且前提是其前面至少有一位数字。
  • 参数中的前导空格将被修剪并忽略。
  • 如果字符串以 "Infinity""-Infinity" 开头且前面没有或多个空格,则 parseFloat() 还可以解析并返回 Infinity-Infinity
  • parseFloat() 从头开始选择最长的子字符串,生成有效的数字文字。如果遇到无效字符,它将返回到该点为止表示的数字,忽略无效字符及其后面的所有字符。
  • 如果参数的第一个字符无法按照上述语法开始合法的数字文字,则 parseFloat 返回 NaN

在语法方面,parseFloat() 解析 Number() 函数接受的语法的子集。即,parseFloat() 不支持带有 0x0b0o 前缀的非十进制文字,但支持其他所有内容。但是,parseFloat()Number() 更宽松,因为它忽略尾随无效字符,这会导致 Number() 返回 NaN

¥Syntax-wise, parseFloat() parses a subset of the syntax that the Number() function accepts. Namely, parseFloat() does not support non-decimal literals with 0x, 0b, or 0o prefixes but supports everything else. However, parseFloat() is more lenient than Number() because it ignores trailing invalid characters, which would cause Number() to return NaN.

与数字文字和 Number() 类似,由于浮点范围和不准确性,从 parseFloat() 返回的数字可能不完全等于字符串表示的数字。对于 -1.7976931348623158e+3081.7976931348623158e+308 范围之外的数字(请参阅 Number.MAX_VALUE),返回 -InfinityInfinity

¥Similar to number literals and Number(), the number returned from parseFloat() may not be exactly equal to the number represented by the string, due to floating point range and inaccuracy. For numbers outside the -1.7976931348623158e+3081.7976931348623158e+308 range (see Number.MAX_VALUE), -Infinity or Infinity is returned.

示例

¥Examples

使用 parseFloat()

¥Using parseFloat()

以下示例均返回 3.14

¥The following examples all return 3.14:

js
parseFloat(3.14);
parseFloat("3.14");
parseFloat("  3.14  ");
parseFloat("314e-2");
parseFloat("0.0314E+2");
parseFloat("3.14some non-digit characters");
parseFloat({
  toString() {
    return "3.14";
  },
});

parseFloat() 返回 NaN

¥parseFloat() returning NaN

以下示例返回 NaN

¥The following example returns NaN:

js
parseFloat("FF2");

有趣的是,由于字符串 NaN 本身是 parseFloat() 接受的无效语法,因此传递 "NaN" 也会返回 NaN

¥Anecdotally, because the string NaN itself is invalid syntax as accepted by parseFloat(), passing "NaN" returns NaN as well.

js
parseFloat("NaN"); // NaN

返回无穷大

¥Returning Infinity

当数字超出双精度 64 位 IEEE 754-2019 格式范围时,将返回无穷大值:

¥Infinity values are returned when the number is outside the double-precision 64-bit IEEE 754-2019 format range:

js
parseFloat("1.7976931348623159e+308"); // Infinity
parseFloat("-1.7976931348623159e+308"); // -Infinity

当字符串以 "Infinity""-Infinity" 开头时,也会返回无穷大:

¥Infinity is also returned when the string starts with "Infinity" or "-Infinity":

js
parseFloat("Infinity"); // Infinity
parseFloat("-Infinity"); // -Infinity

与 BigInt 值的交互

¥Interaction with BigInt values

parseFloat() 不处理 BigInt 值。它在 n 字符处停止,并将前面的字符串视为普通整数,但可能会损失精度。如果将 BigInt 值传递给 parseFloat(),它将转换为字符串,并且该字符串将被解析为浮点数,这也可能导致精度损失。

¥parseFloat() does not handle BigInt values. It stops at the n character, and treats the preceding string as a normal integer, with possible loss of precision. If a BigInt value is passed to parseFloat(), it will be converted to a string, and the string will be parsed as a floating-point number, which may result in loss of precision as well.

js
parseFloat(900719925474099267n); // 900719925474099300
parseFloat("900719925474099267n"); // 900719925474099300

你应该将字符串传递给 BigInt() 函数,而不带尾随 n 字符。

¥You should pass the string to the BigInt() function instead, without the trailing n character.

js
BigInt("900719925474099267");
// 900719925474099267n

规范

Specification
ECMAScript Language Specification
# sec-parsefloat-string

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看