Android常用数据类型(String 、int、float、double、)转换

Android常用数据类型(String 、int、float、double、long)转换

因为经常用到类型装换,所以整理一下,方便查找:

一、String转int,float,double,long.

int i = Integer.parseInt(str) 或者 int i = Integer.valueOf(str).intValue();

float f = Float.parseFloat(str) 或者 float f = Float.valueOf(str).floatValue();

double d = Double.parseDouble(str) 或者 double d = Double.valueOf(str).doubleValue();

long l = Long.parseLong(str) 或者 long l = Long.valueOf(str).longValue();

byte b = Byte.parseByte(str);

short t = Short.parseShort(str);

二、int转String .

String s = String.valueOf(value); // 其中 value 为任意一种数字类型
String s = String.valueOf(i);
String s = Integer.toString(i);
String s = " " + value; // 其中 value 为任意一种数字类型

注: Double, Float, Long 转成字串的方法大同小异

三、float 转int ,String.

float f= 0.0f;

int b = (int)f;//用(int)强制转换为整型b
int b = Math.round(f);//采用round方式转换为整型

String s= String.valueOf(f);
String s= f + “”;
String s= f.toString();

猜你喜欢

转载自blog.csdn.net/yue_233/article/details/91815620