parseint函数_parseInt()函数以及JavaScript中的示例

parseint函数

The function parseInt() is used to convert a string to the integer or we can say it can be used to parse a string and returns an integer.

函数parseInt()用于将字符串转换为整数,或者可以说可以将其用于解析字符串并返回整数。

Some of the points to remember:

需要记住的几点:

  • If a string contains number at the starting it can parse and returns the number.

    如果字符串开头包含数字,则可以解析并返回数字。

  • If a string contains alphabets at the starting and then the number, it cannot parse and returns "NaN".

    如果字符串开头包含字母,然后包含数字,则无法解析并返回“ NaN”

  • If a string contains leading and trailing spaces still function works and parse the string.

    如果字符串包含前导和尾随空格,则函数仍然起作用并解析该字符串。

  • As we have discussed in the post convert hexadecimal string to the decimalparseInt() function parses the octal formatted value by passing base 8 and hexadecimal value by passing base 16 to the number.

    正如我们在后文中讨论的那样, 将十六进制字符串转换为十进制parseInt()函数通过将基数8传递给八进制格式值,并通过将基数16传递给数字来解析十六进制值。

Example:

例:

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

<body>
	<script>
		var str1 = "123";	//decoimal string
		var str2 = "12.23";	//float string
		var str3 = "123 456 789" //multiple Numbers 
		var str4 = "  123  "; //string contains leading & trailing spaces
		var str5 = "012" ; //octal string
		var str6 = "0x0A"; //hexadecimal string
		var str7 = "123456 is my password"; //string contains Number in starting
		var str8 = "My password is 123456"; //string contains Number in ending
		
		//parsing & pritning the values
		document.write("Number value of str1: " + parseInt(str1) + "<br>");
		document.write("Number value of str2: " + parseInt(str2) + "<br>");
		document.write("Number value of str3: " + parseInt(str3) + "<br>");
		document.write("Number value of str4: " + parseInt(str4) + "<br>");
		document.write("Number value of str5: " + parseInt(str5,8) + "<br>");
		document.write("Number value of str6: " + parseInt(str6,16) + "<br>");
		document.write("Number value of str7: " + parseInt(str7) + "<br>");
		document.write("Number value of str8: " + parseInt(str8) + "<br>");
	</script>
</body>
</html>

Output

输出量

Number value of str1: 123
Number value of str2: 12
Number value of str3: 123
Number value of str4: 123
Number value of str5: 10
Number value of str6: 10
Number value of str7: 123456
Number value of str8: NaN


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

parseint函数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值