javascript parseInt与Number函数的区别


Posted in Javascript onJanuary 21, 2010

但是parseInt("08", 10)是可以返回8的。

为搞清楚两者的区别,

参考了别人写的parseInt&Number的区别:

parseInt
Parses a string argument and returns an integer of the specified radix or base.
核心函数
实现版本 Navigator 2.0: If the first character of the string specified in parseInt(string) cannot be converted to a number, returns "NaN" on Solaris and Irix and 0 on all other platforms.Navigator 3.0, LiveWire 2.0: Returns "NaN" on all platforms if the first character of the string specified in parseInt(string) cannot be converted to a number.

语法
parseInt(string,radix)
参数
string A string that represents the value you want to parse.
radix (Optional) An integer that represents the radix of the return value.

描述
The parseInt function is a built-in JavaScript function.
The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values.

If the radix is not specified or is specified as 0, JavaScript assumes the following:

If the input string begins with "0x", the radix is 16 (hexadecimal).

If the input string begins with "0", the radix is eight (octal).

If the input string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseInt returns "NaN".
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".

示例
The following示例 all return 15:
parseInt("F", 16)
parseInt("17", 8)
parseInt("15", 10)
parseInt(15.99, 10)
parseInt("FXX123", 16)
parseInt("1111", 2)
parseInt("15*3", 10) The following示例 all return "NaN":

parseInt("Hello", 8)
parseInt("0x7", 10)
parseInt("FFF", 10) Even though the radix is specified differently, the following示例 all return 17 because the input string begins with "0x".

parseInt("0x11", 16)
parseInt("0x11", 0)
parseInt("0x11")
-----------------------------------------------
-----------------------------------------------
将指定对象转换为数字。
核心函数
实现版本 Navigator 4.0, Netscape Server 3.0

语法
Number(obj)
参数
obj 一个对象。

描述
如果对象是 Date 类型的对象,Number 将返回自格林威治标准时间 1970 年 1 月 1 日起已经经过的毫秒数,在此日期之后的是正数,之前的是负数。
如果 obj 是一个没有数字格式的字符串,Number 将返回 NaN。

示例
下面的例子将把 Date 对象转换为数值型值:
<SCRIPT>
d = new Date ("December 17, 1995 03:24:00");
document.write (Number(d) + "<BR>");

Javascript 相关文章推荐
JavaScript脚本性能优化注意事项
Nov 18 Javascript
WEB前端开发都应知道的jquery小技巧及jquery三个简写
Nov 15 Javascript
基于jquery二维码生成插件qrcode
Jan 07 Javascript
微信小程序 基础组件与导航组件详细介绍
Feb 21 Javascript
JS优化与惰性载入函数实例分析
Apr 06 Javascript
vue之nextTick全面解析
May 17 Javascript
收集前端面试题之url、href、src
Mar 22 Javascript
jQuery实现ajax回调函数带入参数的方法示例
Jun 26 jQuery
原来JS还可以这样拆箱转换详解
Feb 01 Javascript
使用 Vue-TCB 快速在 Vue 应用中接入云开发的方法
Feb 10 Javascript
js实现滑动进度条效果
Aug 21 Javascript
基于javascript实现移动端轮播图效果
Dec 21 Javascript
js parsefloat parseint 转换函数
Jan 21 #Javascript
jquery 防止表单重复提交代码
Jan 21 #Javascript
javascript 哈希表(hashtable)的简单实现
Jan 20 #Javascript
JS 对象介绍
Jan 20 #Javascript
JavaScript 学习笔记(十一)
Jan 19 #Javascript
9个JavaScript评级/投票插件
Jan 18 #Javascript
jQuery Flash/MP3/Video多媒体插件
Jan 18 #Javascript
You might like
PHP中使用正则表达式提取中文实现笔记
2015/01/20 PHP
详谈PHP程序Laravel 5框架的优化技巧
2016/07/18 PHP
php中的explode()函数实例介绍
2019/01/18 PHP
PHP7创建COOKIE和销毁COOKIE的实例方法
2020/02/03 PHP
基于jquery tab切换(防止页面刷新)
2012/05/23 Javascript
JavaScript调用客户端的可执行文件(示例代码)
2013/11/28 Javascript
JS对话框_JS模态对话框showModalDialog用法总结
2014/01/11 Javascript
JS替换字符串中字符即替换全部而不是第一个
2014/06/04 Javascript
node.js超时timeout详解
2014/11/26 Javascript
nodejs开发微博实例
2015/03/25 NodeJs
JavaScript数据结构与算法之栈与队列
2016/01/29 Javascript
Jquery判断form表单数据是否变化
2016/03/30 Javascript
zepto与jquery的区别及zepto的不同使用8条小结
2016/07/28 Javascript
js中怎么判断两个字符串相等的实例
2019/01/17 Javascript
Node.js Windows Binary二进制文件安装方法
2019/05/16 Javascript
javascript异步处理与Jquery deferred对象用法总结
2019/06/04 jQuery
vue实现百度语音合成的实例讲解
2019/10/14 Javascript
构建Vue大型应用的10个最佳实践(小结)
2019/11/07 Javascript
jQuery操作事件完整实例分析
2020/01/10 jQuery
python字符串排序方法
2014/08/29 Python
python使用cPickle模块序列化实例
2014/09/25 Python
python实现在pickling的时候压缩的方法
2014/09/25 Python
python3生成随机数实例
2014/10/20 Python
Python爬虫利用cookie实现模拟登陆实例详解
2017/01/12 Python
Python使用pyh生成HTML文档的方法示例
2018/03/10 Python
python的中异常处理机制
2018/08/30 Python
Python的垃圾回收机制详解
2019/08/28 Python
Python搭建代理IP池实现接口设置与整体调度
2019/10/27 Python
Python FTP文件定时自动下载实现过程解析
2019/11/12 Python
PyTorch加载预训练模型实例(pretrained)
2020/01/17 Python
后端开发使用pycharm的技巧(推荐)
2020/03/27 Python
三月法制宣传月活动总结
2014/07/03 职场文书
办理信用卡收入证明范例
2014/09/13 职场文书
pandas进行数据输入和输出的方法详解
2022/03/23 Python
Python 一键获取电脑浏览器的账号密码
2022/05/11 Python
MYSQL如何查看操作日志详解
2022/05/30 MySQL