javascript parseInt&Number函數的區別

來源:互聯網
上載者:User

在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>");  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.