當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Javascript parseInt()用法及代碼示例


parseInt()是JavaScript中的內置函數,用於接受字符串並將其轉換為整數。它實際上返回指定基數(基數)的整數,該整數被接受為parseInt()函數的第二個參數。如果字符串不包含數值,則返回NaN,即不是數字。句法:

parseInt(Value, radix)

參數:它接受下麵指定的兩個參數-

  • Value:“Value”包含一個字符串,該字符串將轉換為整數。
  • radix:此參數表示要使用的基數或基數,它是可選的。

返回值:它返回一個數字,如果第一個字符不能轉換為數字,則該函數返回NaN。它實際上返回一個解析的數字,直到遇到一個不是指定基數(基數)的數字的字符為止。
例:


Input: var n = parseInt("2018@geeksforgeeks");
Output: n = 2018
now n contains 2018 as '@' is not a Number 
and parsing stops at that point,further characters are ignored.
Input: var a = parseInt("1000");
Output: a = 1000(Number)

JavaScript,顯示parseInt()函數的工作方式:

代碼1:
<!DOCTYPE html> 
<html> 
<body> 
    <script> 
      
        a = parseInt("100"); 
        document.write('parseInt("100") = ' + a + "<br>"); 
          
        // It returns a Integer until 
        // it encounters Not a Number character 
        b = parseInt("2018@geeksforgeeks"); 
        document.write('parseInt("2018@geeksforgeeks") = ' + b + "<br>"); 
          
        // It returns NaN on Non numeral character 
        c = parseInt("geeksforgeeks@2018"); 
        document.write('parseInt("geeksforgeeks@2018") = ' + c + "<br>"); 
          
        // It returns Integer value of a Floating point Number 
        d = parseInt("3.14"); 
        document.write('parseInt("3.14") = ' + d + "<br>"); 
          
        // It returns only first Number it encounters 
        e = parseInt("21 7 2018"); 
        document.write('parseInt("21 7 2018") = ' + e + "<br>"); 
      
    </script> 
  
</body> 
</html>                    

輸出:

parseInt("100") = 100
parseInt("2018@geeksforgeeks") = 2018
parseInt("geeksforgeeks@2018") = NaN
parseInt("3.14") = 3
parseInt("21 7 2018") = 21

代碼2:
如果parseInt()函數中未提及基數,並且字符串的開頭包含“0x”,則該數字將其視為十六進製值。默認情況下,基數為10(十進製)。請注意,在第11行中有一個“ 8”,這是在基數8數字係統中未定義的字符,因此它返回NaN。

<!DOCTYPE html> 
<html> 
    <body> 
        <script> 
          
        //base 10 
        a = parseInt("100",10); 
        document.write('parseInt("100",10) = ' + a + "<br>"); 
          
        //base 8 
        b = parseInt("8",8); 
        document.write('parseInt("8",8) = ' + b + "<br>"); 
          
        //base 8 
        c = parseInt("15",8); 
        document.write('parseInt("15",8) = ' + c + "<br>"); 
          
        //base 16 
        d = parseInt("16",16); 
        document.write('parseInt("16",16) = ' + d + "<br>"); 
          
        // Leading and trailing spaces are ignored in parseInt() function 
        e = parseInt(" 100 "); 
        document.write('parseInt(" 100 ") = ' + e + "<br>"); 
          
        //base 16(hexadecimal) 
        f = parseInt("0x16"); 
        document.write('parseInt("0x16") = ' + f + "<br>"); 
          
        </script> 
      
    </body> 
</html>                                

輸出:

parseInt("100",10) = 100
parseInt("8",8) = NaN
parseInt("15",8) = 13
parseInt("16",16) = 22
parseInt(" 100 ") = 100
parseInt("0x16") = 22


相關用法


注:本文由純淨天空篩選整理自vivekkothari大神的英文原創作品 JavaScript | parseInt() with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。