java基础知识2

1.java字符串转为int型:

String id= 1int id_int = Integer.parseInt(id);

2.java 字符串转为bool类型:

parseBoolean函数:如果 String 参数不是 null 且在忽略大小写时等于"true",则返回的 boolean 表示 true 值。

String flag1 = “true”;
String flag1 = “false”;
Boolean.parseBoolean(flag1);//将字符串转为布尔类型  
Boolean.parseBoolean(flag2);//将字符串转为布尔类型   
System.out.println(flag1);
System.out.println(flag2);

运行结果:

3.java 两个int数相除,返回一个保留两位小数的数:

DecimalFormat df=new DecimalFormat("0.00");//java保留两位小数
int shoes_count = 100000;
int count = 600;
System.out.println("shoes_count:"+shoes_count);
System.out.println("count:"+count);
System.out.println("未使用DecimalFormat类:"+shoes_count/count);
System.out.println("使用DecimalFormat类:"+df.format((float)shoes_count/count));

运行结果:

注意:df.format()返回的为字符串。

4.java字符串转为float:

String s = "1.23";
System.out.println(Float.parseFloat(s)+1);

运行结果:

原文地址:https://www.cnblogs.com/qilin20/p/12291679.html