当前位置: 首页 > 工具软件 > toNumber > 使用案例 >

to_number用法示例_Number()函数以及JavaScript中的示例

郎和通
2023-12-01

to_number用法示例

Number()函数 (Number() function)

Number() function is a predefined global function in JavaScript, it used to convert an object to the number. If the function is not able to convert the object in a number – it returns "NaN". (Read more: NaN in JavaScript).

Number()函数是JavaScript中预定义的全局函数,用于将对象转换为数字。 如果函数无法将对象转换为数字,则返回“ NaN” 。 (了解更多: JavaScript中的NaN )。

Note:

注意:

  • If we convert Boolean values to the number, Number() function returns 0 or 1.

    如果将布尔值转换为数字,则Number()函数将返回0或1。

  • If we convert a date object to the number, Number() function returns value in milliseconds.

    如果我们将日期对象转换为数字,则Number()函数将返回以毫秒为单位的值。

Example:

例:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>		
		var a = "10";	
		var b = "10 20";
		var c = "1234 Hello";
		var d = "Hello 1234";
		var e = true;	
		var f = new Date();
		
		//converting values to number using Number() function
		document.write("Number(a) = " + Number(a) + "<br>");
		document.write("Number(b) = " + Number(b) + "<br>");
		document.write("Number(c) = " + Number(c) + "<br>");
		document.write("Number(d) = " + Number(d) + "<br>");
		document.write("Number(e) = " + Number(e) + "<br>");
		document.write("Number(f) = " + Number(f) + "<br>");
	</script>
</body>
</html>

Output

输出量

Number(a) = 10
Number(b) = NaN
Number(c) = NaN
Number(d) = NaN
Number(e) = 1
Number(f) = 1549128507537

翻译自: https://www.includehelp.com/code-snippets/Number-function-with-example-in-javascript.aspx

to_number用法示例

 类似资料: