Java 中类型转换


int -> String

int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

String -> int

s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();

String -> float

Float.parseFloat(name)

String -> Date

java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd ");
String s= "2011-07-09 "; 
Date date =  formatter.parse(s);

Date->String

java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd ");
String date = formatter.format(new Date());//格式化数据

BigDecimal<-->String

String StrBd="1048576.1024";

BigDecimal bd=new BigDecimal(StrBd);        // 转换为bigdecimal

bd=bd.setScale(2, BigDecimal.ROUND_HALF_UP);    //设置小数位数,第一个变量是小数位数,第二个变量是取舍方法(四舍五入)

 

String OutString=bd.toString();      //转化为字符串

Long<-->int

long lll = 300000;  
int ii = (int)lll;

int iii= new Long(ll).intValue();

l2=(long)i; 

l=new Long((long)i);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM