首页 > 编程语言 > 详细

JAVA基本类型的转换

时间:2016-10-19 20:17:56      阅读:292      评论:0      收藏:0      [点我收藏+]

1、String转成Int

例1:
String str = "123";
try {
    int a = Integer.parseInt(str);
} catch (NumberFormatException e) {
    e.printStackTrace();
}
例2:
String str = "123";
try {
    int b = Integer.valueOf(str).intValue()
} catch (NumberFormatException e) {
    e.printStackTrace();
}

2、Double转成Int

例1:
double   d   =   12.0;   
int   i   =   (new   Double(d)).intValue();
例2:
double   d   =   12.0;   
int   i   =   (int)d;   

3、Integer转成String

Integer it = new Integer(10);

String str = it.toString();

4、Integer转int

Integer转换成int的方法

Integer i = new Integer(10); 
int k = i.intValue();
即Integer.intValue();

5、int转Integer

int转换成Integer

int i = 10;

Integer it = new Integer(i);

6、String转BigDecimal

String转换成BigDecimal

BigDecimal bd = new BigDecimal(str);

 7、double类型转成String顺便格式化

double wcl=0.00;//完成率
String zzwcl=new java.text.DecimalFormat("0.00").format(wcl);//最终完成率  double装换成String顺便格式化

 

JAVA基本类型的转换

原文:http://www.cnblogs.com/xinxin1994/p/5978337.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!