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 相关文章推荐
jquery获取url参数及url加参数的方法
Oct 26 Javascript
jQuery防止重复绑定事件的解决方法
May 14 Javascript
jquery判断checkbox是否选中及改变checkbox状态的实现方法
May 26 Javascript
利用Angularjs和原生JS分别实现动态效果的输入框
Sep 01 Javascript
AngularJS中关于ng-class指令的几种实现方式详解
Sep 17 Javascript
Javascript中this绑定的3种方法与比较
Oct 13 Javascript
jQuery中值得注意的trigger方法浅析
Dec 12 Javascript
微信小程序本作用域下调用全局JS详解及实例
Feb 22 Javascript
学习使用Bootstrap栅格系统
May 11 Javascript
JS监听事件的叠加和移除功能
Nov 19 Javascript
Vue简单实现原理详解
May 07 Javascript
基于Web Audio API实现音频可视化效果
Jun 12 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在线代理转向代码
2012/05/05 PHP
php操作mysqli(示例代码)
2013/10/28 PHP
PHP操作FTP类 (上传、下载、移动、创建等)
2016/03/31 PHP
详细解读php的命名空间(二)
2018/02/21 PHP
浅析Js(Jquery)中,字符串与JSON格式互相转换的示例(直接运行实例)
2013/07/09 Javascript
jQuery浏览器CSS3特写兼容实例
2015/01/19 Javascript
详解基于Vue cli生成的Vue项目的webpack4升级
2018/06/19 Javascript
JavaScript实现动态添加、移除元素或属性的方法分析
2019/01/03 Javascript
Vue中多个元素、组件的过渡及列表过渡的方法示例
2019/02/13 Javascript
Vue.js特性Scoped Slots的浅析
2019/02/20 Javascript
小程序自定义单页面、全局导航栏的实现代码
2019/03/15 Javascript
vue 项目build错误异常的解决方法
2019/04/22 Javascript
vue项目接口域名动态获取操作
2020/08/13 Javascript
js+canvas实现画板功能
2020/09/13 Javascript
Python批量更改文件名的实现方法
2017/10/29 Python
python实现读Excel写入.txt的方法
2018/04/29 Python
对pytorch网络层结构的数组化详解
2018/12/08 Python
利用ctypes获取numpy数组的指针方法
2019/02/12 Python
Python 使用type来定义类的实现
2019/11/19 Python
Django ORM 查询表中某列字段值的方法
2020/04/30 Python
python装饰器三种装饰模式的简单分析
2020/09/04 Python
医疗保健专业人士购物网站:Scrubs & Beyond
2017/02/08 全球购物
西班牙太阳镜品牌:Hawkers
2018/03/11 全球购物
Becextech新西兰:数码单反相机和手机在线商店
2018/04/27 全球购物
美国领先的家庭健康检测试剂盒提供商:LetsGetChecked
2019/03/18 全球购物
shell程序中如何注释
2012/01/28 面试题
战友聚会邀请函
2014/01/18 职场文书
幼儿教师工作感言
2014/02/14 职场文书
个人校本研修方案
2014/05/26 职场文书
综艺节目策划方案
2014/06/13 职场文书
房屋租赁合同解除协议书
2014/10/11 职场文书
停电通知范文
2015/04/16 职场文书
党员公开承诺书(2016最新版)
2016/03/24 职场文书
python删除csv文件的行列
2021/04/06 Python
基于HTML十秒做出淘宝页面
2021/10/24 HTML / CSS
Mysql数据库手动及定时备份步骤
2021/11/07 MySQL