数字转换为字符串,字符串转换为数字

 步骤 1 : 

数字转字符串

方法1: 使用String类的静态方法valueOf 
方法2: 先把基本类型装箱为对象,然后调用对象的toString

代码比较复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package digit;

  

public class TestNumber {

  

    public static void main(String[] args) {

        int i = 5;

         

        //方法1

        String str = String.valueOf(i);

         

        //方法2

        Integer it = i;

        String str2 = it.toString();

         

    }

}

 步骤 2 : 

字符串转数字

调用Integer的静态方法parseInt

代码比较复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package digit;

  

public class TestNumber {

  

    public static void main(String[] args) {

        String str = "999";

         

        int i= Integer.parseInt(str);

         

        System.out.println(i);

         

    }

}

猜你喜欢

转载自blog.csdn.net/Whiteleaf3er/article/details/82020747
今日推荐