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 相关文章推荐
JS 建立对象的方法
Apr 21 Javascript
node.js中的http.response.write方法使用说明
Dec 14 Javascript
如何解决easyui自定义标签 datagrid edit combobox 手动输入保存不上
Dec 26 Javascript
jQuery each函数源码分析
May 25 Javascript
AngularJs bootstrap搭载前台框架——准备工作
Sep 01 Javascript
AngularJS $injector 依赖注入详解
Sep 14 Javascript
js实现对table的增加行和删除行的操作方法
Oct 13 Javascript
node使用UEditor富文本编辑器的方法实例
Jul 11 Javascript
react-native fetch的具体使用方法
Nov 01 Javascript
关于angularJs清除浏览器缓存的方法
Nov 28 Javascript
详解Node.js 中使用 ECDSA 签名遇到的坑
Nov 26 Javascript
JS实现给数组对象排序的方法分析
Jun 24 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
linux iconv方法的使用
2011/10/01 PHP
Discuz!X中SESSION机制实例详解
2015/09/23 PHP
PHP Ajax JavaScript Json获取天气信息实现代码
2016/08/17 PHP
Laravel学习教程之model validation的使用示例
2017/10/23 PHP
PHP常见的几种攻击方式实例小结
2019/04/29 PHP
laravel-admin 后台表格筛选设置默认的查询日期方法
2019/10/03 PHP
javascript取消文本选定的实现代码
2010/11/14 Javascript
如何获取select下拉框的值(option没有及有value属性)
2013/11/08 Javascript
Jquery使用css方法改变样式实例
2015/05/18 Javascript
JavaScript的Backbone.js框架环境搭建及Hellow world示例
2016/05/07 Javascript
基于HTML5上使用iScroll实现下拉刷新,上拉加载更多
2016/05/21 Javascript
js获取Get值的方法
2016/09/29 Javascript
js实现右键菜单功能
2016/11/28 Javascript
简单实现bootstrap导航效果
2017/02/07 Javascript
nodejs实现邮件发送服务实例分享
2017/03/29 NodeJs
bootstrap弹出层的多种触发方式
2017/05/10 Javascript
微信小程序 es6-promise.js封装请求与处理异步进程
2017/06/12 Javascript
Vue 项目代理设置的优化
2018/04/17 Javascript
详解jQuery中的easyui
2018/09/02 jQuery
React+Redux实现简单的待办事项列表ToDoList
2019/09/29 Javascript
javascript设计模式 ? 单例模式原理与应用实例分析
2020/04/09 Javascript
[18:32]DOTA2 HEROS教学视频教你分分钟做大人-谜团
2014/06/12 DOTA
[06:15]2016国际邀请赛中国区预选赛单车采访:我顶WINGS
2016/06/27 DOTA
基于python实现微信好友数据分析(简单)
2020/02/16 Python
Python matplotlib实时画图案例
2020/04/23 Python
完美解决ARIMA模型中plot_acf画不出图的问题
2020/06/04 Python
python开发入门——set的使用
2020/09/03 Python
美国复古街头服饰精品店:Need Supply Co.
2017/02/22 全球购物
加拿大约会网站:EliteSingles.ca
2018/01/12 全球购物
Juicy Couture Beauty官方网站:香水和化妆品
2019/03/12 全球购物
办理居住证介绍信
2014/01/15 职场文书
党员公开承诺事项
2014/03/25 职场文书
教研处工作方案
2014/05/26 职场文书
法制宣传口号
2014/06/16 职场文书
婚礼父母致辞
2015/07/28 职场文书
电力安全教育培训心得体会
2016/01/11 职场文书