★ wanayoo — archive 1999 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/parseIntNouvelle recherche | Portail wanayoo

Notice: Some MDN email addresses and encrypted passwords were temporarily posted on a publicly accessible server. http://blog.mozilla.org/security/2014/08/01/mdn-database-disclosure/

您的搜索结果

    parseInt

    概述

    parseInt() 函数将给定的字符串以指定基数(radix/base)解析成为整数。

    语法

    parseInt(string, radix);
    

    参数

    string
    被解析的值。如果不是一个字符串,则将其转换为字符串。字符串开头的空白符将会被忽略。
    radix
    一个整数值,指定转换中采用的基数。总是指定该参数可以保证结果可预测。当忽略该参数时,不同的实现环境可能产生不同的结果。

    描述

    parseInt 顶级函数,没有与任何对象关联。

    The parseInt 函数将第一个参数(字符串)解析并试图返回一个整数特定基数的整数。例如:基数10 将会转换成十进制数,8 对应八进制,16 对应十六进制,等等。基数大于 10 时,用字母表中的字母来表示大于 9 的数字。例如十六进制中,使用 A 到 F。

    如果 parseInt 遇到了不属于特定基数的字符那么该字符和其后的字符都将被忽略。接着返回已经解析的整数部分。parseInt 将截取整数部分。开头和结尾的空白符允许存在,会被忽略。

    在没有指定基数,或者基数为 0 的情况下,JavaScript 作如下处理:

    • 字符串 string 以"0x"开头, 基数是16 (16进制).
    • 字符串 string 以"0"开头, 基数是8(8进制)。这个特性不赞成。
    • 字符串 string 以其它任何值开头,基数是10 (十进制)。

    如果第一个字符不能被转换成数字,parseInt 返回NaN。

    算术上, NaN 不是一个数值。 你可以调用isNaN 来判断 parseInt 是否返回 NaN。NaN 参与的数学运算其结果总是 NaN。

    将整型数值以特定基数转换成它的字符串值可以使用 intValue.toString(radix).

    示例

    例子:使用 parseInt

    都返回15:

    parseInt("F", 16);
    parseInt("17", 8);
    parseInt("15", 10);
    parseInt(15.99, 10);
    parseInt("FXX123", 16);
    parseInt("1111", 2);
    parseInt("15*3", 10);
    parseInt("12", 13);
    

    都返回 NaN:

    parseInt("Hello", 8); // Not a number at all
    parseInt("546", 2);   // Digits are not valid for binary representations
    

    下面全都返回 -15:

    parseInt("-F", 16);
    parseInt("-0F", 16);
    parseInt("-0XF", 16);
    parseInt(-15.1, 10);
    parseInt(" -17", 8);
    parseInt(" -15", 10);
    parseInt("-1111", 2);
    parseInt("-15e1", 10);
    parseInt("-12", 13);

    下例中也全部返回 17,因为输入的 string 参数以 "0x" 开头时作为十六进制数字解释,而第二个参数假如经过 Number 函数转换后为 0 或 NaN,则将会忽略。

    parseInt("0x11", 16);
    parseInt("0x11", 0);
    parseInt("0x11");
    

    没有指定 radix 参数时的八进制解释

    尽管 ECMAScript 3 已经不赞成这种做法,且 ECMAScript 5, 已经进制了这种做法,但是仍然有很多实现环境仍然吧以 0 开头的数值字符串(numeric string)解释为一个八进制数。下面的例子,一个返回八进制结果,一个返回十进制结果。总是指定一个基数(radix)可以避免这种不可靠的行为。

    parseInt("0e0"); // 0 parseInt("08"); // 0, '8' is not an octal digit.

    ECMAScript 5 移除了八进制的解释

    ECMAScript 5 规范中 parseInt 函数部分不在允许实现环境把以 0 字符开始的字符串作为八进制数值了。ECMAScript 5 陈述如下:

    The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. If radix is 16, number may also optionally begin with the character pairs 0x or 0X.

    This differs from ECMAScript 3, which discouraged but allowed octal interpretation.

    Many implementations have not adopted this behavior as of 2013, and because older browsers must be supported, always specify a radix.

    一个更严格的解析函数

    有时采用一个更严格的方法来解析整型值很有用。此时正则表达式很有用:

    filterInt = function (value) {
      if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
        return Number(value);
      return NaN;
    }
    
    console.log(filterInt('421'));               // 421
    console.log(filterInt('-421'));              // -421
    console.log(filterInt('+421'));              // 421
    console.log(filterInt('Infinity'));          // Infinity
    console.log(filterInt('421e+0'));            // NaN
    console.log(filterInt('421hop'));            // NaN
    console.log(filterInt('hop1.61803398875'));  // NaN
    console.log(filterInt('1.61803398875'));     // NaN
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    规范

    规范版本 规范状态 注解
    ECMAScript 1st Edition. Standard Initial definition.
    ECMAScript 5.1 (ECMA-262)
    parseInt
    Standard  
    ECMAScript 6 (ECMA-262)
    parseInt
    Draft  

    浏览器兼容性

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

    相关链接

     

    文档标签和贡献者

    对本页有贡献的人: AlexChao, Mickeyboy, teoli
    最后编辑者: AlexChao,