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 相关文章推荐
js获取单选按钮的数据
Nov 27 Javascript
javascript GUID生成器实现代码
Oct 31 Javascript
jQuery总体架构的理解分析
Mar 07 Javascript
JavaScript获取FCK编辑器信息的具体方法
Jul 12 Javascript
JavaScript判断变量是对象还是数组的方法
Aug 28 Javascript
javascript精确统计网站访问量实例代码
Dec 19 Javascript
jQuery实现图片轮播效果代码
Sep 27 Javascript
jQuery插件zTree实现获取当前选中节点在同级节点中序号的方法
Mar 08 Javascript
javascript 封装Date日期类实例详解
May 28 Javascript
对于js垃圾回收机制的理解
Sep 14 Javascript
基于滚动条位置判断的简单实例
Dec 14 Javascript
基于JS+HTML实现弹窗提示是否确认提交功能
Jun 17 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中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思
2013/06/28 PHP
实例讲解yii2.0在php命令行中运行的步骤
2015/12/01 PHP
Yii2针对游客、用户防范规则和限制的解决方法分析
2016/10/08 PHP
jQuery使用手册之三 CSS操作
2007/03/24 Javascript
跨浏览器通用、可重用的选项卡tab切换js代码
2011/09/20 Javascript
JavaScript 匿名函数(anonymous function)与闭包(closure)
2011/10/04 Javascript
js实现网页标题栏闪烁提示效果实例分析
2014/11/20 Javascript
基于Bootstrap实现tab标签切换效果
2020/04/15 Javascript
JS实现线性表的链式表示方法示例【经典数据结构】
2017/04/11 Javascript
Nodejs回调加超时限制两种实现方法
2017/06/09 NodeJs
基于JS实现仿京东搜索栏随滑动透明度渐变效果
2017/07/10 Javascript
详解vue2.0 不同屏幕适配及px与rem转换问题
2018/02/23 Javascript
vue2 设置router-view默认路径的实例
2018/09/20 Javascript
jQuery事件多次绑定与解绑问题实例分析
2019/02/19 jQuery
使用vue2.6实现抖音【时间轮盘】屏保效果附源码
2019/04/24 Javascript
JavaScript命令模式原理与用法实例详解
2020/03/10 Javascript
vue在App.vue文件中监听路由变化刷新页面操作
2020/08/14 Javascript
[01:08:29]DOTA2-DPC中国联赛定级赛 RNG vs Aster BO3第一场 1月9日
2021/03/11 DOTA
Python基础语法(Python基础知识点)
2016/02/28 Python
浅析Python 引号、注释、字符串
2019/07/25 Python
python批量图片处理简单示例
2019/08/06 Python
python实现单张图像拼接与批量图片拼接
2020/03/23 Python
PyCharm+Pipenv虚拟环境开发和依赖管理的教程详解
2020/04/16 Python
详解Django ORM引发的数据库N+1性能问题
2020/10/12 Python
Python 按比例获取样本数据或执行任务的实现代码
2020/12/03 Python
使用CSS3的ruby-position固定注音位置的用法示例
2016/07/05 HTML / CSS
为什么要做架构设计
2015/07/08 面试题
档案接收函
2014/01/13 职场文书
农村改厕实施方案
2014/03/22 职场文书
2014年党员个人剖析材料
2014/10/08 职场文书
单位考核聘任报告
2015/03/02 职场文书
增值税发票丢失证明
2015/06/19 职场文书
运动会闭幕式致辞
2015/07/29 职场文书
宝宝满月祝酒词
2015/08/10 职场文书
2016关于军训的心得体会
2016/01/11 职场文书
2019请假条的基本格式及范文!
2019/07/05 职场文书