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调试说明
Jun 07 Javascript
将string解析为json的几种方式小结
Nov 11 Javascript
情人节专属 纯js脚本1k大小的3D玫瑰效果
Feb 11 Javascript
jquery动态改变form属性提交表单
Jun 03 Javascript
JS获取iframe中marginHeight和marginWidth属性的方法
Apr 01 Javascript
javascript实现3D切换焦点图
Oct 16 Javascript
AngularJS中实现动画效果的方法
Jul 28 Javascript
详解使用nvm管理多版本node的方法
Aug 30 Javascript
JavaScript变量声明var,let.const及区别浅析
Apr 23 Javascript
简单了解Ajax表单序列化的实现方法
Jun 14 Javascript
JS多个表单数据提交下的serialize()应用实例分析
Aug 27 Javascript
JavaScript canvas实现跟随鼠标事件
Feb 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
DOM基础及php读取xml内容操作的方法
2015/01/23 PHP
php实现的一个简单json rpc框架实例
2015/03/30 PHP
php对二维数组进行相关操作(排序、转换、去空白等)
2015/11/04 PHP
CodeIgniter视图使用注意事项
2016/01/20 PHP
php mysqli查询语句返回值类型实例分析
2016/06/29 PHP
js Html结构转字符串形式显示代码
2011/11/15 Javascript
文本域光标操作的jQuery扩展分享
2014/03/10 Javascript
jquery实现页面百叶窗走马灯式翻滚显示效果的方法
2015/03/12 Javascript
jQuery插件开发精品教程让你的jQuery提升一个台阶
2016/01/27 Javascript
jquery拖拽排序简单实现方法(效果增强版)
2016/02/16 Javascript
AngularJS 单元测试(一)详解
2016/09/21 Javascript
JS日期对象简单操作(获取当前年份、星期、时间)
2016/10/26 Javascript
QRCode.js:基于JQuery的生成二维码JS库的使用
2017/06/23 jQuery
JavaScript代码执行的先后顺序问题
2017/10/29 Javascript
Vue.js 踩坑记之双向绑定
2018/05/03 Javascript
Vue单页及多页应用全局配置404页面实践记录
2018/05/22 Javascript
jQuery实现适用于移动端的跑马灯抽奖特效示例
2019/01/18 jQuery
深入学习TypeScript 、React、 Redux和Ant-Design的最佳实践
2019/06/17 Javascript
Vue的状态管理vuex使用方法详解
2020/02/05 Javascript
javascript canvas封装动态时钟
2020/09/30 Javascript
[13:38]2015国际邀请赛中国战队出征仪式
2015/05/29 DOTA
python实现下载指定网址所有图片的方法
2015/08/08 Python
Python多层嵌套list的递归处理方法(推荐)
2016/06/08 Python
python3 unicode列表转换为中文的实例
2018/10/26 Python
Python数据可视化:幂律分布实例详解
2019/12/07 Python
Python sys模块常用方法解析
2020/02/20 Python
Python matplotlib模块及柱状图用法解析
2020/08/10 Python
css3中用animation的steps属性制作帧动画
2019/04/25 HTML / CSS
纯CSS3实现滚动的齿轮动画效果
2014/06/05 HTML / CSS
旅游管理专业学生求职信
2013/09/28 职场文书
销售人员求职信
2014/07/22 职场文书
护士岗位竞聘书
2015/09/15 职场文书
如何写一份具有法律效力的借款协议书?
2019/07/02 职场文书
入门学习Go的基本语法
2021/07/07 Golang
python playwright 自动等待和断言详解
2021/11/27 Python
2021年国漫热度排行前十,完美世界上榜,第四是美国动画作品
2022/03/18 国漫