当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JavaScript parseFloat() 方法

JavaScript
JS 介绍
JS 怎样使用
JS 在哪使用
JS 变量
JS If...Else
JS Switch
JS 操作符
JS Popup Boxes
JS 函数
JS For 循环
JS While 循环
JS Break 循环
JS For...In
JS 事件
JS Try...Catch
JS Throw
JS onerror
JS 特殊字符
JS Guidelines
JS 对象介绍

JavaScript parseFloat() 方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 445 ::
收藏到网摘: n/a

Definition and Usage
定义与用法

The parseFloat() function parses a string and returns a floating point number.
parseFloat()方法用于将一个字符串转换为浮点形数字。

This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.
本方法取决于字符串的首字符是否为数字。如果是,则执行到第一个非数字字符时结束,并将结果返回为数字。

Syntax
语法

parseFloat(string)

Parameter
参数
Description
注释
string
字符串
Required. The string to be parsed
必选项。所要转换的字符串


Tips and Notes
注意

Note: Only the first number in the string is returned!
注意:只返回第一个连续的数字。

Note: Leading and trailing spaces are allowed.
注意:字符串可以包含空格。

Note: If the first character cannot be converted to a number, parseFloat() returns NaN.
注意:如果字符串的首字符不能被转换为数字,则返回NaN。


Example
实例

In this example we will use parseFloat() to parse different strings:
在下面的例子中,我们将演示如何用parseFloat()来转换不同的字符串:

<script type="text/javascript">
document.write(parseFloat("10") + "<br />") 
document.write(parseFloat("10.00") + "<br />")
document.write(parseFloat("10.33") + "<br />")
document.write(parseFloat("34 45 66") + "<br />")
document.write(parseFloat(" 60 ") + "<br />")
document.write(parseFloat("40 years") + "<br />")
document.write(parseFloat("He was 40") + "<br />")
</script>

The output of the code above will be:
输出结果为:

10
10
10.33
34
60
40
NaN


Try-It-Yourself Demos
互动演练

parseFloat()
How to use parseFloat() to parse different strings.
如何用parseFloat()转换不同的字符串。

评论 (0) All

登陆 | 还没注册?