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 相关文章推荐
基于prototype的validation.js发布2.3.4新版本,让你彻底脱离表单验证的烦恼
Dec 06 Javascript
用Div仿showModalDialog模式菜单的效果的代码
Mar 05 Javascript
jquery购物车实时结算特效实现思路
Sep 23 Javascript
鼠标移到div,浮层显示明细,弹出层与div的上边距左边距重合(示例代码)
Dec 14 Javascript
js与C#进行时间戳转换
Nov 14 Javascript
利用CSS、JavaScript及Ajax实现图片预加载的方法
Nov 29 Javascript
微信小程序中使用javascript 回调函数
May 11 Javascript
jquery实现图片轮播器
May 23 jQuery
利用canvas实现的加载动画效果实例代码
Jul 05 Javascript
React注册倒计时功能的实现
Sep 06 Javascript
Vue项目实现换肤功能的一种方案分析
Aug 28 Javascript
jQuery操作元素的内容和样式完整实例分析
Jan 10 jQuery
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
discuz论坛 用户登录 后台程序代码
2008/11/27 PHP
PHP字符串word末字符实现大小写互换的方法
2014/11/10 PHP
js+CSS 图片等比缩小并垂直居中实现代码
2008/12/01 Javascript
javascript Array.sort() 跨浏览器下需要考虑的问题
2009/12/07 Javascript
Jquery跨域获得Json时invalid label错误的解决办法
2011/01/11 Javascript
javascript实现图片切换的幻灯片效果源代码
2012/12/12 Javascript
js下将阿拉伯数字每三位一逗号分隔(如:15000000转化为15,000,000)
2014/06/02 Javascript
Jquery中offset()和position()的区别分析
2015/02/05 Javascript
jQuery实现二级下拉菜单效果
2016/01/05 Javascript
js内置对象处理_打印学生成绩单的简单实现
2016/09/24 Javascript
jQuery和JavaScript节点插入元素的方法对比
2016/11/18 Javascript
Bootstrap导航条鼠标悬停下拉菜单
2017/01/04 Javascript
JavaScript实现无穷滚动加载数据
2017/05/06 Javascript
vue之数据交互实例代码
2017/06/20 Javascript
解决easyui日期时间框ie的兼容的问题
2018/03/01 Javascript
vue项目设置scrollTop不起作用(总结)
2018/12/21 Javascript
详解Vue中的scoped及穿透方法
2019/04/18 Javascript
解决layui checkbox 提交多个值的问题
2019/09/02 Javascript
ES6学习教程之Promise用法详解
2020/11/22 Javascript
关于uniApp editor微信滑动问题
2021/01/15 Javascript
通过滑动翻页效果实现和移动端click事件问题
2021/01/26 Javascript
Python合并两个字典的常用方法与效率比较
2015/06/17 Python
Pandas实现一列数据分隔为两列
2020/05/18 Python
基于Canvas+Vue的弹幕组件的实现
2019/07/23 HTML / CSS
Dyson加拿大官方网站:购买戴森吸尘器,风扇,冷热器及配件
2016/10/26 全球购物
英国自行车商店:AW Cycles
2021/02/24 全球购物
Araks官网:纽约内衣品牌
2020/10/15 全球购物
法国购买二手电子产品网站:Asgoodasnew
2020/03/27 全球购物
学习全国两会精神心得体会范文
2014/03/17 职场文书
寒假家长评语大全
2014/04/16 职场文书
总经理岗位职责说明书
2014/07/30 职场文书
2014年个人委托书范本
2014/10/13 职场文书
五一劳动节活动总结
2015/02/09 职场文书
大学军训通讯稿(2016最新版)
2015/12/21 职场文书
100句拼搏进取的名言警句,值得一读!
2019/10/07 职场文书
导游词之张家口
2019/12/13 职场文书