javascript parseInt与Number函数的区别

 更新时间:2010年01月21日 22:36:11   作者:  
在js中,如果你使用parseInt("08"),一般都会认为会返回8,然而实际上返回了0.但是用Number("08")返回的才是8.
但是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>");

相关文章

  • function foo的原型与prototype属性解惑

    function foo的原型与prototype属性解惑

    最近在研究js,疑惑也比较多。主要是被原型这个东西给弄迷糊了。
    2010-11-11
  • JS中队列和双端队列实现及应用详解

    JS中队列和双端队列实现及应用详解

    这篇文章主要介绍了JS中队列和双端队列实现及应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • onkeydown事件解决按回车键直接提交数据的需求

    onkeydown事件解决按回车键直接提交数据的需求

    登陆页面需要扑捉用户按下回车自动提交的需求,于是相到在body里添加onkeydown事件跳javascript在提交表单,具体看下实现代码,希望对你有所帮助
    2013-04-04
  • JS中三种URI编码方式对比分析

    JS中三种URI编码方式对比分析

    这篇文章主要介绍了JS中三种URI编码方式对比分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2024-01-01
  • es6中class类静态方法,静态属性,实例属性,实例方法的理解与应用分析

    es6中class类静态方法,静态属性,实例属性,实例方法的理解与应用分析

    这篇文章主要介绍了es6中class类静态方法,静态属性,实例属性,实例方法的理解与应用,结合实例形式分析了es6 class类静态方法,静态属性,实例属性,实例方法相关概念、原理、用法及相关操作注意事项,需要的朋友可以参考下
    2020-02-02
  • js tab栏切换代码实例解析

    js tab栏切换代码实例解析

    这篇文章主要介绍了js tab栏切换代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • knockoutjs模板实现树形结构列表

    knockoutjs模板实现树形结构列表

    这篇文章主要介绍了knockoutjs模板实现树形结构列表的实现代码,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-07-07
  • JS常用倒计时代码实例总结

    JS常用倒计时代码实例总结

    这篇文章主要介绍了JS常用倒计时代码,结合实例形式总结分析了JS常用的倒计时功能实现方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • js获取鼠标位置杂谈附多浏览器兼容代码

    js获取鼠标位置杂谈附多浏览器兼容代码

    最近在搞一个AJAX的小功能,目的是用浮动div框显示当前鼠标下控件的详细信息,其中获得鼠标位置这块害得我走了很多冤枉路,因为压根没有想到我下面提到的第二点的区别,所以我的页面出来总是找不到我之前定义的那个div
    2008-11-11
  • 什么是JavaScript注入攻击?

    什么是JavaScript注入攻击?

    本文告诉大家什么是js注入,讨论防止 ASP.NET MVC 应用程序受到 JavaScript 注入攻击的两种技术,感兴趣的小伙伴们可以参考一下
    2016-09-09

最新评论