Android String类型转换为float、double和int的工具类

时间:2022-10-16 15:57:41
public class ConvertUtil {

	//把String转化为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转化为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转化为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;
		}
	}
}