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 相关文章推荐
javascript EXCEL 操作类代码
Jul 30 Javascript
JS清除IE浏览器缓存的方法
Jul 26 Javascript
jQuery针对各类元素操作基础教程
Aug 29 Javascript
IE6浏览器中window.location.href无效的解决方法
Nov 20 Javascript
js实现类似新浪微博首页内容渐显效果的方法
Apr 10 Javascript
ztree获取选中节点时不能进入可视区域出现BUG如何解决
Dec 03 Javascript
Bootstrap模态框插件使用详解
May 11 Javascript
EasyUI Datebox 日期验证之开始日期小于结束时间
May 19 Javascript
微信小程序五子棋游戏的棋盘,重置,对弈实现方法【附demo源码下载】
Feb 20 Javascript
微信小程序中悬浮窗功能的实现代码
Aug 02 Javascript
es6函数之rest参数用法实例分析
Apr 18 Javascript
JS实现简易贪吃蛇游戏
Aug 24 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 全局变量范围分析
2009/08/07 PHP
php实现约瑟夫问题的方法小结
2015/03/23 PHP
PHP使用递归方式列出当前目录下所有文件的方法
2015/06/02 PHP
PHP实现简单数字分页效果
2015/07/26 PHP
利用php实现一周之内自动登录存储机制(cookie、session、localStorage)
2016/10/31 PHP
thinkPHP中钩子的两种配置调用方法详解
2016/11/11 PHP
PHP7.1实现的AES与RSA加密操作示例
2018/06/15 PHP
Yii2框架加载css和js文件的方法分析
2019/05/25 PHP
PHP延迟静态绑定使用方法实例解析
2020/09/05 PHP
javascript 字符串连接的性能问题(多浏览器)
2008/11/18 Javascript
jquery 模式对话框终极版实现代码
2009/09/28 Javascript
基于JQuery的一句代码实现表格的简单筛选
2010/07/26 Javascript
jQuery实现table隔行换色和鼠标经过变色的两种方法
2014/06/15 Javascript
js鼠标点击图片切换效果实现代码
2015/11/19 Javascript
7个去伪存真的JavaScript面试题
2016/01/07 Javascript
jquery实现列表上下移动功能
2016/02/25 Javascript
Angular.js中定时器循环的3种方法总结
2017/04/27 Javascript
JS实现合并json对象的方法
2017/10/10 Javascript
微信小程序实现人脸检测功能
2018/05/25 Javascript
vue-router的使用方法及含参数的配置方法
2018/11/13 Javascript
vue路由中前进后退的一些事儿
2019/05/18 Javascript
bootstrap Table实现合并相同行
2019/07/19 Javascript
解决vue cli使用typescript后打包巨慢的问题
2019/09/30 Javascript
微信小程序后端无法保持session的原因及解决办法问题
2020/03/20 Javascript
JavaScript 实现自己的安卓手机自动化工具脚本(推荐)
2020/05/13 Javascript
详解Webpack4多页应用打包方案
2020/07/16 Javascript
vantUI 获得piker选中值的自定义ID操作
2020/11/04 Javascript
Python处理文本换行符实例代码
2018/02/03 Python
Python IDLE清空窗口的实例
2018/06/25 Python
将Django项目部署到CentOs服务器中
2018/10/18 Python
Python为何不支持switch语句原理详解
2020/10/21 Python
常见的软件开发流程有哪些
2015/11/14 面试题
一名毕业生的自我鉴定
2013/12/04 职场文书
学生逃课万能检讨书2000字
2015/02/17 职场文书
2016年会开场白台词
2015/06/01 职场文书
Python+Appium新手教程
2021/04/17 Python