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 相关文章推荐
XMLHTTPRequest的属性和方法简介
Nov 23 Javascript
jQuery制作简洁的多级联动Select下拉框
Dec 23 Javascript
jQuery实现“扫码阅读”功能
Jan 21 Javascript
javascript实现表单验证
Jan 29 Javascript
jquery跟随屏幕滚动效果的实现代码
Apr 13 Javascript
jQuery学习笔记之入门
Dec 14 Javascript
Bootstrap选项卡学习笔记分享
Feb 13 Javascript
angular中如何绑定iframe中src的方法
Feb 01 Javascript
vue中格式化时间过滤器代码实例
Apr 17 Javascript
Vue搭建后台系统需要注意的问题
Nov 08 Javascript
在vue中使用axios实现post方式获取二进制流下载文件(实例代码)
Dec 16 Javascript
Layer UI表格列日期格式化及取消自动填充日期的实现方法
May 10 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
水质对咖图啡风味的影响具体有哪些
2021/03/03 冲泡冲煮
十天学会php之第十天
2006/10/09 PHP
php strnatcmp()函数的用法总结
2013/11/27 PHP
PHP模拟post提交数据方法汇总
2016/02/16 PHP
PHP封装的数据库保存session功能类
2016/07/11 PHP
javascript使用中为什么10..toString()正常而10.toString()出错呢
2013/01/11 Javascript
javascript获得网页窗口实际大小的示例代码
2013/09/21 Javascript
js如何判断用户是否是用微信浏览器
2014/06/05 Javascript
js完美实现@提到好友特效(兼容各大浏览器)
2015/03/16 Javascript
jQuery解决IE6、7、8不能使用 JSON.stringify 函数的问题
2016/05/31 Javascript
vue微信分享 vue实现当前页面分享其他页面
2017/12/02 Javascript
浅谈vue自定义全局组件并通过全局方法 Vue.use() 使用该组件
2017/12/07 Javascript
使用koa-log4管理nodeJs日志笔记的使用方法
2018/11/30 NodeJs
jQuery实现网页拼图游戏
2020/04/22 jQuery
关于vue-cli 3配置打包优化要点(推荐)
2019/04/22 Javascript
JavaScript 正则应用详解【模式、欲查、反向引用等】
2020/05/13 Javascript
js实现3D旋转相册
2020/08/02 Javascript
解决antd日期选择组件,添加value就无法点击下一年和下一月问题
2020/10/29 Javascript
微信小程序抽奖组件的使用步骤
2021/01/11 Javascript
[05:17]DOTA2睡衣妹卖萌求签名 CJ第二天全明星影像
2013/07/28 DOTA
python数组复制拷贝的实现方法
2015/06/09 Python
W3C公布最新的HTML5标准草案
2008/10/17 HTML / CSS
美国高端婴童品牌:Hanna Andersson
2016/10/30 全球购物
世界上最大的高分辨率在线图片库:Alamy
2018/07/07 全球购物
大学毕业生通用求职信
2013/09/28 职场文书
土木工程专业个人求职信
2013/12/30 职场文书
中秋节礼品促销方案
2014/02/02 职场文书
售后服务经理岗位职责
2014/02/25 职场文书
三年级小学生评语
2014/04/22 职场文书
机关党建工作汇报材料
2014/08/20 职场文书
保险公司演讲稿
2014/09/02 职场文书
学校运动会广播稿100条
2014/09/14 职场文书
2014年乡镇党建工作总结
2014/11/11 职场文书
上课睡觉检讨书300字
2014/11/18 职场文书
就业推荐表导师评语
2014/12/31 职场文书
《认识年月日》教学反思
2016/02/19 职场文书