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 相关文章推荐
textarea的value是html文件源代码,存成html文件的代码
Apr 20 Javascript
JQuery 学习笔记 选择器之六
Jul 23 Javascript
jquery 学习之二 属性相关
Nov 23 Javascript
网站如何做到完全不需要jQuery也可以满足简单需求
Jun 27 Javascript
javascript实现跳转菜单的具体方法
Jul 05 Javascript
Javascript打印局部页面实例
Jun 21 Javascript
Javascript快速实现浏览器系统通知
Aug 26 Javascript
JavaScript实现随机数生成器(去重)
Oct 13 Javascript
vue项目首屏加载时间优化实战
Apr 23 Javascript
小程序富文本提取图片可放大缩小
May 26 Javascript
js模拟实现百度搜索
Jun 28 Javascript
完美解决vue 中多个echarts图表自适应的问题
Jul 19 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面向对象之旅:深入理解static变量与方法
2014/01/06 PHP
php实现mysql连接池效果实现代码
2018/01/25 PHP
javascript操作css属性
2013/12/30 Javascript
jquery转盘抽奖功能实现
2015/11/13 Javascript
JavaScript实现iframe自动高度调整和不同主域名跨域
2016/02/27 Javascript
Bootstrap3 input输入框插入glyphicon图标的方法
2016/05/16 Javascript
详解js实现线段交点的三种算法
2016/08/09 Javascript
JS中作用域和变量提升(hoisting)的深入理解
2016/10/31 Javascript
Angular1.x复杂指令实例详解
2017/03/01 Javascript
nodejs搭建本地http服务器教程
2017/03/13 NodeJs
vue.js组件之间传递数据的方法
2017/07/10 Javascript
jQuery实现定时隐藏对话框的方法分析
2018/02/12 jQuery
jquery动态添加带有样式的HTML标签元素方法
2018/02/24 jQuery
详解Ubuntu安装angular-cli遇到的坑
2018/09/08 Javascript
4个顶级JavaScript高级文本编辑器
2018/10/10 Javascript
Angular PWA使用的Demo示例
2019/01/31 Javascript
微信小程序在线客服自动回复功能(基于node)
2019/07/03 Javascript
TypeScript 引用资源文件后提示找不到的异常处理技巧
2020/07/15 Javascript
vue中的.$mount('#app')手动挂载操作
2020/09/02 Javascript
[58:15]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 NB vs Liquid
2018/04/02 DOTA
Python闭包之返回函数的函数用法示例
2018/01/27 Python
Python 比较文本相似性的方法(difflib,Levenshtein)
2018/10/15 Python
对Python3使运行暂停的方法详解
2019/02/18 Python
Python编写合并字典并实现敏感目录的小脚本
2019/02/26 Python
使用 python pyautogui实现鼠标键盘控制功能
2019/08/04 Python
python 实现让字典的value 成为列表
2019/12/16 Python
已安装tensorflow-gpu,但keras无法使用GPU加速的解决
2020/02/07 Python
css3实现图片遮罩效果鼠标hover以后出现文字
2013/11/05 HTML / CSS
美国户外运动商店:Sun & Ski
2018/08/23 全球购物
《学会待客》教学反思
2014/02/22 职场文书
报关报检委托书
2014/04/08 职场文书
“四风”查摆问题自我剖析材料
2014/09/27 职场文书
2015年化验室工作总结
2015/04/23 职场文书
辣妈辣妹观后感
2015/06/10 职场文书
Java 实现限流器处理Rest接口请求详解流程
2021/11/02 Java/Android
Redis实现短信验证码登录的示例代码
2022/06/14 Redis