public class ConvertUtil {
//把String轉(zhuǎn)化為float
public static float convertToFloat(String number, float defaultValue) {
if (TextUtils.isEmpty(number)) {
return defaultValue;
}
try {
return Float.parseFloat(number);
} catch (Exception e) {
return defaultValue;
}
}
//把String轉(zhuǎn)化為double
public static double convertToDouble(String number, double defaultValue) {
if (TextUtils.isEmpty(number)) {
return defaultValue;
}
try {
return Double.parseDouble(number);
} catch (Exception e) {
return defaultValue;
}
}
//把String轉(zhuǎn)化為int
public static int convertToInt(String number, int defaultValue) {
if (TextUtils.isEmpty(number)) {
return defaultValue;
}
try {
return Integer.parseInt(number);
} catch (Exception e) {
return defaultValue;
}
}
---------------------
使用時(shí)只需調(diào)用上面的方法即可(第二個(gè)參數(shù)是默認(rèn)值):
int yeatInt = ConvertUtil.convertToInt("2017",2015);