JavaScript parseint()理解

來源:互聯網
上載者:User

JavaScript parseint()理解

js parseint()函數

 

工作中,在網關設定MTU值,見到這個函數。當時用法:

 

function saveMtu()
{
var maxMtu;
with (document.forms[0])
{
maxMtu = parseInt(mtu.value);


if(isNaN(mtu.value))
{
alert(MTU值 ! + mtu.value + 非法,取值範圍(68--1500)!);
return 0;
}


if(maxMtu < 68 || maxMtu > 1500)
{
alert(MTU值 ! + mtu.value + 非法,取值範圍(68--1500)!);
return 0;
}
loc += &maxMtu= + maxMtu;
}
return 1;
}

 

查看W3C,解釋為:可解析一個字串,並返回一個整數。

 

parseInt(string, radix)
string 為必選,要解析的字串。

 

radix 為可選,解析的基數,類似於,我要以十進位,16進位解析,該參數小於 2 或者大於 36,則 parseInt() 將返回 NaN

 

 

parseInt(10);//返回 10parseInt(19,10);//返回 19 (10+9) (十進位)parseInt(11,2);//返回 3 (2+1) (2進位左1為2,右1為1,所以為2+1)parseInt(17,8);//返回 15 (8+7) (8進位,17等於左1為8,右7為7,所以8+7)
parseInt(2f,16);//返回 47 (16進位,2*16+15=47)parseInt(010);//未定:返回 10 或 8

 

 

例如:

 


列印出:

10
29
7
23
47
10
NaN

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.