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 相关文章推荐
用于自动添加Digg This!按钮的JavaScript
Dec 23 Javascript
IE event.srcElement和FF event.target 功能比较
Mar 01 Javascript
基于Jquery与WebMethod投票功能实现代码
Jan 19 Javascript
关于div自适应高度/左右高度自适应一致的js代码
Mar 22 Javascript
jquery可见性过滤选择器使用示例
Jun 24 Javascript
js中的异常处理try...catch使用介绍
Sep 21 Javascript
php中给js数组赋值方法
Mar 10 Javascript
JavaScript数组的一些奇葩行为
Jan 25 Javascript
最常见的左侧分类菜单栏jQuery实现代码
Nov 28 Javascript
DropDownList实现可输入可选择(两种版本可选)
Dec 07 Javascript
node解析修改nginx配置文件操作实例分析
Nov 06 Javascript
JS前端基于canvas给图片添加水印
Nov 11 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
在WAMP环境下搭建ZendDebugger php调试工具的方法
2011/07/18 PHP
YII动态模型(动态表名)支持分析
2016/03/29 PHP
类之Prototype.js学习
2007/06/13 Javascript
解析img图片没找到onerror事件 Stack overflow at line: 0
2013/12/23 Javascript
jquery遍历函数siblings()用法实例
2015/12/24 Javascript
总结在前端排序中遇到的问题
2016/07/19 Javascript
基于JavaScript实现飘落星星特效
2017/08/10 Javascript
详解如何将 Vue-cli 改造成支持多页面的 history 模式
2017/11/20 Javascript
Node.js一行代码实现静态文件服务器的方法步骤
2019/05/07 Javascript
微信小程序+腾讯地图开发实现路径规划绘制
2019/05/22 Javascript
jquery css实现流程进度条
2020/03/26 jQuery
JS箭头函数和常规函数之间的区别实例分析【 5 个区别】
2020/05/27 Javascript
Vue $attrs &amp; inheritAttr实现button禁用效果案例
2020/12/07 Vue.js
vue绑定class的三种方法
2020/12/24 Vue.js
[00:12]2018DOTA2亚洲邀请赛 Somnus丶M出阵单挑
2018/04/06 DOTA
[47:22]Mineski vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python实现把数字转换成中文
2015/06/29 Python
浅谈Python由__dict__和dir()引发的一些思考
2017/10/30 Python
Python实现PS滤镜中马赛克效果示例
2018/01/20 Python
Python字符串格式化%s%d%f详解
2018/02/02 Python
Python进阶之自定义对象实现切片功能
2019/01/07 Python
python使用phoenixdb操作hbase的方法示例
2019/02/28 Python
Python提取特定时间段内数据的方法实例
2019/04/01 Python
我喜欢你 抖音表白程序python版
2019/04/07 Python
基于python框架Scrapy爬取自己的博客内容过程详解
2019/08/05 Python
python自动发微信监控报警
2019/09/06 Python
使用Python实现微信拍一拍功能的思路代码
2020/07/09 Python
Python unittest生成测试报告过程解析
2020/09/08 Python
加拿大租车网站:Enterprise Rent-A-Car
2018/07/26 全球购物
企业厂长岗位职责
2013/12/17 职场文书
社区先进事迹材料
2014/05/19 职场文书
法院信息化建设方案
2014/05/21 职场文书
支部鉴定材料
2014/06/02 职场文书
民事答辩状范本
2015/05/21 职场文书
大学生村官工作心得体会
2016/01/23 职场文书
如何利用 CSS Overview 面板重构优化你的网站
2021/10/24 HTML / CSS