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 相关文章推荐
InnerHtml和InnerText的区别分析
Mar 13 Javascript
jQuery实现统计复选框选中数量
Nov 24 Javascript
Bootstrap3制作自己的导航栏
May 12 Javascript
JavaScript实现256色转灰度图
Feb 22 Javascript
jQuery实现分页功能(含ajax请求、后台数据、附完整demo)
Apr 03 jQuery
Angularjs 实现动态添加控件功能
May 25 Javascript
基于input框覆盖掉数字英文的实例讲解
Jul 21 Javascript
javascript实现获取一个日期段内每天不同的价格(计算入住总价格)
Feb 05 Javascript
JS抛物线动画实例制作
Feb 24 Javascript
jQuery层叠选择器用法实例分析
Jun 28 jQuery
解决Idea、WebStorm下使用Vue cli脚手架项目无法使用Webpack别名的问题
Oct 11 Javascript
用Javascript实现发送短信验证码间隔功能
Feb 08 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
Yii框架结合sphinx,Ajax实现搜索分页功能示例
2016/10/18 PHP
PHP 实现base64编码文件上传出现问题详解
2020/09/01 PHP
jquery cookie插件代码类
2009/05/26 Javascript
javascript 二分法(数组array)
2010/04/24 Javascript
JQUERY获取form表单值的代码
2010/07/17 Javascript
基于jQuery的一个扩展form序列化到json对象
2010/12/09 Javascript
javascript 利用Image对象实现的埋点(某处的点击数)统计
2012/12/28 Javascript
javascript从image转换为base64位编码的String
2014/07/29 Javascript
Jquery的基本对象转换和文档加载用法实例
2015/02/25 Javascript
JQuery使用index方法获取Jquery对象数组下标的方法
2015/05/18 Javascript
原生JS实现的放大镜效果实例代码
2016/10/15 Javascript
网页中右键功能的实现方法之contextMenu的使用
2017/02/20 Javascript
浅谈js-FCC算法Friendly Date Ranges(详解)
2017/04/10 Javascript
详解vue-cli 构建Vue项目遇到的坑
2017/08/30 Javascript
浅析从vue源码看观察者模式
2018/01/29 Javascript
详解React-Router中Url参数改变页面不刷新的解决办法
2018/05/08 Javascript
JavaScript递归函数定义与用法实例分析
2019/01/24 Javascript
JS实现li标签的删除
2019/04/12 Javascript
微信小程序错误this.setData报错及解决过程
2019/09/18 Javascript
NodeJS和浏览器中this关键字的不同之处
2021/03/03 NodeJs
vue中axios封装使用的完整教程
2021/03/03 Vue.js
python检测lvs real server状态
2014/01/22 Python
python异常和文件处理机制详解
2016/07/19 Python
Pycharm编辑器技巧之自动导入模块详解
2017/07/18 Python
Django中的CBV和FBV示例介绍
2018/02/25 Python
python爬虫面试宝典(常见问题)
2018/03/02 Python
使用Python将语音转换为文本的方法
2020/08/10 Python
用HTML5制作一个简单的弹力球游戏
2015/05/12 HTML / CSS
html5 postMessage前端跨域并前端监听的方法示例
2018/11/01 HTML / CSS
北京某公司的.net笔试题
2014/03/20 面试题
助人为乐表扬信范文
2014/01/14 职场文书
巾帼志愿者活动方案
2014/08/17 职场文书
签订劳动合同通知书
2015/04/16 职场文书
画展观后感
2015/06/17 职场文书
python爬虫之利用selenium模块自动登录CSDN
2021/04/22 Python
《艾尔登法环》1.03.3补丁上线 碎星伤害调整
2022/04/06 其他游戏