From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/global_objects/parsefloat/index.html | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/parsefloat') diff --git a/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html b/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html new file mode 100644 index 0000000000..6469340da8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html @@ -0,0 +1,123 @@ +--- +title: parseFloat +slug: Web/JavaScript/Reference/Global_Objects/parseFloat +tags: + - parseFloat() +translation_of: Web/JavaScript/Reference/Global_Objects/parseFloat +--- +
+
+
{{jsSidebar("Objects")}}
+
+
+ +

parseFloat() 函数解析一个参数(必要时先转换为字符串)并返回一个浮点数。

+ +
{{EmbedInteractiveExample("pages/js/globalprops-parsefloat.html")}}
+ +

语法

+ +
parseFloat(string)
+ +

参数

+ +
+
string
+
需要被解析成为浮点数的值。
+
+ +

返回值

+ +
+
给定值被解析成浮点数。如果给定值不能被转换成数值,则会返回 {{jsxref("NaN")}}。
+
+ +

描述

+ +

parseFloat是个全局函数,不属于任何对象。

+ + + +

考虑使用 {{jsxref("Number", "Number(value)")}} 进行更严谨的解析,只要参数带有无效字符就会被转换为 {{jsxref("NaN")}} 。

+ +

parseFloat 也可以转换一个已经定义了 toString 或者 valueOf 方法的对象,它返回的值和在调用该方法的结果上调用 parseFloat 值相同。

+ +

例子

+ +

例子: parseFloat返回正常数字

+ +

下面的例子都返回3.14

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

parseFloat返回NaN

+ +

下面的例子将返回NaN

+ +
parseFloat("FF2");
+
+ +

parseFloat 和 BigInt

+ +

以下例子均返回 900719925474099300,当整数太大以至于不能被转换时将失去精度。

+ +
parseFloat(900719925474099267n);
+parseFloat('900719925474099267n');
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
ECMAScript 1st Edition.StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.1.2.3', 'parseFloat')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-parsefloat-string', 'parseFloat')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-parsefloat-string', 'parseFloat')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ +

{{Compat("javascript.builtins.parseFloat")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf