javascript parseInt与Number函数的区别

但是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>");

(0)

相关推荐

  • JavaScript的parseInt 取整使用

    Java 也有 Integer.parseInt() 方法, 但是 JavaScript 的 parseInt 处理方式与 Java 等强整型语言不太一样, 所以经常有人因为对这个方法的使用不当而获得异常返回. 下面是一段 Java 代码, 用于将字符串 020 转为整型. 复制代码 代码如下: public class Test { public static void main(String args[]) throws Exception { String str = "020"

  • 关于javascript中的parseInt使用技巧

    要对表单中填写的日期格式进行客户端验证,于是在网上找了段代码,其中用到parseInt对年月日做判断,其中有类似这样的语句: ...... else if(parseInt(month)<1 || parseInt(month) >12) ...... 可是对于当前本来正确的日期,这里怎么也通不过.后来一查才明白,parseInt实际上有两个参数,第一个是要转换的值,第二个是指定的进制.如果不指定第二个参数,那么它只能正确地转换01到07(即把它们转换成1到7),从08开始,它就会按照&quo

  • 详解js中Number()、parseInt()和parseFloat()的区别

    一:Number() 如果是Boolean值,true和false值将分别被转换为1和0. 如果是数字值,只是简单的传入和返回. 如果是null值,返回0. 如果是undefined,返回NaN. 如果是字符串: a.  如果字符串中只包含数字时,将其转换为十进制数值,忽略前导0 b. 如果字符串中包含有效浮点格式,如"1.1",将其转换为对应的浮点数字,忽略前导0 c. 如果字符串中包含有效的十六进制格式,如"0xf",将其转换为相同大小的十进制数值 d. 如果字

  • javascript parseInt 函数分析(转)

    javascript的parseInt函数 javascript的parseInt函数,大家都知道是干啥的 但你知道 parseInt("07") 返回多少 ? parseInt("08") 又返回多少 ? 正确答案是 parseInt("07") 返回8 parseInt("08") 返回0 你知道问题在哪? 其实,这个问题可能大家都没想过吧. 用javascript的parseInt函数时, parseInt("

  • javascript中parseInt()函数的定义和用法分析

    本文实例讲述了javascript中parseInt()函数的定义和用法.分享给大家供大家参考.具体分析如下: 此函数可以解析一个字符串,并返回一个整数. 语法结构: 复制代码 代码如下: parseInt(string, type) 参数列表: 参数 描述 string 必需.要被解析的字符串. type 可选.表示要解析的数字的基数,通俗的说就是数字的进制,比如二进制.八进制或者十六进制.该值介于2 ~ 36之间. 详细说明: 一.指定type参数: 指定type参数后,函数就会按照指定的t

  • js parseInt的陷阱分析小结

    复制代码 代码如下: var a = parseInt("09"), b = Number("09"); 很多人会认为a和b的值都是数字9,但实际上不是. parseInt的主要作用是把字符串转换为整数,或者把小数转换为整数.一般情况下,我们只用到它的第一个参数.但实际上,它有两个参数: parseInt(string, radix) parseInt会根据radix指定的进制进行转换,比如: 复制代码 代码如下: alert(parseInt("10&q

  • JS实现手写parseInt的方法示例

    前言 本文主要给大家介绍了关于JS实现手写parseInt的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 手写parseInt的实现:要求简单一些,把字符串型的数字转化为真正的数字即可,但不能使用JS原生的字符串转数字的API,比如Number() 示例代码 function _parseInt(str, radix) { let str_type = typeof str; let res = 0; if (str_type !== 'string' &&

  • javascript中的parseInt和parseFloat区别

    先看代码: 复制代码 代码如下: <script> alert(parseInt("3.54 apples")); alert(parseFloat("3.54 apples")); </script> <script>alert(parseInt("3.54 apples"));alert(parseFloat("3.54 apples"));</script> 运行结果: p

  • javascript parseInt 大改造

    还隐约记得得知了来龙去脉,为自己掌握了一个经验而欢呼雀跃. 还隐约记得被这同一问题折磨了无数次后,无奈与痛下决心的心境. 首先我必须感谢那些即使这个问题我强调过无数次,也依然反复重复类似错误的人们. 没有他们反复犯错的鼓励,或许我不会认真考虑这个问题的解决方案. 其次,必须感谢<JavaScript高级程序设计>的作者和译者. 在这里我得到了解决该问题的启示,不然我依然要每每强调使用parseInt时应注意什么. 同时,希望在这里不仅仅留下一个解决方案. 解决问题的思路与想法,以及对问题举一反

  • js中parseInt函数浅谈

    从很热门的实例parseInt("09")==0说起.parseInt(number,type)这个函数后面如果不跟第2个参数来表示进制的话,默认是10进制.比如说parseInt("010",10)就是10进制的结果:10,parseInt("010",2)就是2进制的结果:2,parseInt("010",8)就是8进制的结果:8,parseInt("010",16)就是2进制的结果:16. 下面我来说

随机推荐