public class NumberParseUtil {
private static final Logger logger = LoggerFactory
.getLogger(NumberParseUtil.class);
public static int parseInt(String str) throws Exception {
if (str == null) {
return 0;
} else {
try {
return Integer.parseInt(str);
} catch (Exception e) {
logger.error("int类型转换错误", e);
throw new Exception();
}
}
}
public static float parseFloat(String str) throws Exception {
if (str == null) {
return 0;
} else {
try {
return Float.parseFloat(str);
} catch (Exception e) {
logger.error("float类型转换错误", e);
throw new Exception();
}
}
}
}
private static final Logger logger = LoggerFactory
.getLogger(NumberParseUtil.class);
public static int parseInt(String str) throws Exception {
if (str == null) {
return 0;
} else {
try {
return Integer.parseInt(str);
} catch (Exception e) {
logger.error("int类型转换错误", e);
throw new Exception();
}
}
}
public static float parseFloat(String str) throws Exception {
if (str == null) {
return 0;
} else {
try {
return Float.parseFloat(str);
} catch (Exception e) {
logger.error("float类型转换错误", e);
throw new Exception();
}
}
}
}
本文介绍了一个实用的Java工具类,用于将字符串安全地转换为整型(int)和浮点型(float)数值。该工具类提供了异常处理机制,并记录了转换过程中的错误。
846

被折叠的 条评论
为什么被折叠?



