码迷,mamicode.com
首页 > 其他好文 > 详细

数字与字符串相互转换

时间:2020-02-01 00:53:07      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:value   style   sys   pre   str   static   print   调用   ring   

数字转字符串

方法一:使用String类的静态方法valueOf()

方法二:先把基本类型装箱为对象,然后调用对象的toString方法

                   

 1 public class TestNumber {
 2   
 3     public static void main(String[] args) {
 4         int i = 5;
 5          
 6         //方法1
 7         String str = String.valueOf(i);
 8          
 9         //方法2
10         Integer it = i;
11         String str2 = it.toString();
12          
13     }
14 }

字符串转数字

调用Integer的静态方法parseInt

 1 public class TestNumber {
 2   
 3     public static void main(String[] args) {
 4  
 5         String str = "999";
 6          
 7         int i= Integer.parseInt(str);
 8          
 9         System.out.println(i);
10          
11     }
12 }

 

数字与字符串相互转换

标签:value   style   sys   pre   str   static   print   调用   ring   

原文地址:https://www.cnblogs.com/z-cg/p/12247183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!