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 相关文章推荐
根据分辨率不同,调用不同的css文件
Jul 07 Javascript
用JavaScrpt实现文件夹简单轻松加密的实现方法图文
Sep 08 Javascript
基于jQuery的图片剪切插件
Aug 03 Javascript
JavaScript代码性能优化总结(推荐)
May 16 Javascript
分享jQuery封装好的一些常用操作
Jul 28 Javascript
原生js编写基于面向对象的分页组件
Dec 05 Javascript
JS身份证信息验证正则表达式
Jun 12 Javascript
JS实现点击Radio动态更新table数据
Jul 18 Javascript
js+html获取系统当前时间
Nov 10 Javascript
详解vuex中action何时完成以及如何正确调用dispatch的思考
Jan 21 Javascript
微信小程序的开发范式BeautyWe.js入门详解
Jul 10 Javascript
vue scroll滚动判断的实现(是否滚动到底部、滚动方向、滚动节流、获取滚动区域dom元素)
Jun 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
模拟OICQ的实现思路和核心程序(一)
2006/10/09 PHP
php之XML转数组函数的详解
2013/06/07 PHP
mongodb和php的用法详解
2019/03/25 PHP
发一个自己用JS写的实用看图工具实现代码
2008/07/26 Javascript
js弹窗代码 可以指定弹出间隔
2010/07/03 Javascript
基于JQuery 滑动与动画的说明介绍
2013/04/18 Javascript
JavaScript获取和设置CheckBox状态的简单方法
2013/07/05 Javascript
解析JavaScript中的不可见数据类型
2013/12/02 Javascript
JavaScript中的公有、私有、特权和静态成员用法分析
2014/11/20 Javascript
浅谈JS日期(Date)处理函数
2014/12/07 Javascript
js获取checkbox值的方法
2015/01/28 Javascript
基于jquery实现人物头像跟随鼠标转动
2015/08/23 Javascript
window.location.hash知识汇总
2015/11/09 Javascript
JavaScript实现下拉菜单的显示和隐藏
2016/01/05 Javascript
基于jquery实现无限级树形菜单
2016/03/22 Javascript
JavaScript来实现打开链接页面的简单实例
2016/06/02 Javascript
详谈Object.defineProperty 及实现数据双向绑定
2020/07/18 Javascript
python计算文本文件行数的方法
2015/07/06 Python
python tkinter界面居中显示的方法
2018/10/11 Python
Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】
2019/10/12 Python
Python图像处理库PIL的ImageDraw模块介绍详解
2020/02/26 Python
python实现测试工具(二)——简单的ui测试工具
2020/10/19 Python
Python 利用Entrez库筛选下载PubMed文献摘要的示例
2020/11/24 Python
pycharm 实现光标快速移动到括号外或行尾的操作
2021/02/05 Python
小程序canvas中文字设置居中锚点
2019/04/16 HTML / CSS
UNIONBAY官网:美国青少年服装品牌
2019/03/26 全球购物
NOTINO英国:在线购买美容和香水
2020/02/25 全球购物
物理系毕业生自荐信
2013/11/01 职场文书
关于迟到的检讨书
2014/01/26 职场文书
人力资源专员岗位职责
2014/01/30 职场文书
会计专业自我鉴定
2014/02/10 职场文书
房地产营销活动策划方案
2014/09/15 职场文书
学校开学标语
2014/10/06 职场文书
《鲸》教学反思
2016/02/23 职场文书
Python 居然可以在 Excel 中画画你知道吗
2022/02/15 Python
openEuler 搭建java开发环境的详细过程
2022/06/10 Servers