parseInt()

parseInt() 方法用于將字符串參數(shù)作為有符號(hào)的十進(jìn)制整數(shù)進(jìn)行解析。

如果方法有兩個(gè)參數(shù), 使用第二個(gè)參數(shù)指定的基數(shù),將字符串參數(shù)解析為有符號(hào)的整數(shù)。

語(yǔ)法

所有 Number 派生類(lèi) parseInt 方法格式類(lèi)似如下:

static int parseInt(String s)static int parseInt(String s, int radix)

參數(shù)

s-- 十進(jìn)制表示的字符串。

radix-- 指定的基數(shù)。(但其實(shí)它不是進(jìn)制,很多人誤以為是進(jìn)制)

返回值

parseInt(String s):返回用十進(jìn)制參數(shù)表示的整數(shù)值。

parseInt(int i):使用指定基數(shù)的字符串參數(shù)表示的整數(shù) (基數(shù)可以是 10, 2, 8, 或 16 等進(jìn)制數(shù)) 。


public static void main(String args[]) {

int x = Integer.parseInt("9");

double c = Double.parseDouble("5");

int b = Integer.parseInt("444",16);

//

//    System.out.println(x);

//    System.out.println(c);

//    System.out.println(b);

//

//    System.out.println(Integer.parseInt("19",10));    //返回 19 (10+9)

//    System.out.println(Integer.parseInt("11",2));    //返回 3 (2+1)

//    System.out.println(Integer.parseInt("17",8));    //返回 15 (8+7)

//    System.out.println(Integer.parseInt("1f",16));    //返回 31 (16+15)

//    System.out.println(Integer.parseInt("010"));

// 1*10^1+0*10^0

      System.out.println(Integer.parseInt("10"));//返回 10

// 1*2^1+1*2^0

      System.out.println(Integer.parseInt("11",2));//返回 3(2+1)

// 1*2^2+1*2^1+1*2^0

      System.out.println(Integer.parseInt("111",2));//返回 7(4+2+1)

// 1*3^2+1*3^1+1*3^0

      System.out.println(Integer.parseInt("111",3));//返回 13(9+3+1)

// 1*4^2+3*4^1+1*4^0

      System.out.println(Integer.parseInt("1132",4));//返回 29

// 1*6^3+1*6^2+3*6^1+1*6^0

      System.out.println(Integer.parseInt("1132",6));//返回 272(216+36+18+2)

// 其中 6 是基數(shù);3次冪是位數(shù)4-1=3;

  }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容