Home > Web Front-end > JS Tutorial > body text

js parseInt 将字符串的开头几个数字转化为int

WBOY
Release: 2016-06-01 09:55:08
Original
2576 people have browsed it

parseInt 将字符串的开头几个数字转化为int,但如果开头不是数字,将返回NaN

定义和用法
parseInt(string, radix) 函数可解析一个字符串,并返回一个整数。

此函数有两个参数,第一个参数是要被解析的字符串,这个参数是必须的, 第二个参数表示要解析的数字的基数,如果省略该参数或其值为 0,则数字将以 10 为基础来解析。如果它以 “0x” 或 “0X” 开头,将以 16 为基数。
如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN。

提示和注释
注释:只有字符串中的第一个数字会被返回。
注释:开头和结尾的空格是允许的。
提示:如果字符串的第一个字符不能被转换为数字,那么 parseFloat() 会返回 NaN。

实例:

<code><script type="text/javascript">
var codeshard=parseInt("125codeshared");//值为125
var codeshard=parseInt("codeshared");// 值为NaN
</script></code>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!