日期:2014-05-17 浏览次数:20754 次
public static int parseInt(String str, int defaultValue)
{
try
{
return Integer.valueOf(str);
}
catch (NumberFormatException ex)
{
return defaultValue;
}
}
public static short parseShort(String str, short defaultValue)
{
...
}
public static long parseLong(String str, long defaultValue)
{
...
}
/**
*
* @author
* @date 2014-4-27
* @param clazz
* @param numberStr
* @param defaultValue
* @return
*/
public static Number conv(Class<? extends Number> clazz, String numberStr, Number defaultValue) {
try {
if(Integer.class.isAssignableFrom(clazz)) {
return Integer.parseInt(numberStr);
} else if(Long.class.isAssignableFrom(clazz)) {
return Long.parseLong(numberStr);
} else if(Short.class.isAssignableFrom(clazz)) {
return Short.parseShort(numberStr);
} else if(Double.class.isAssignableFrom(clazz)) {
return Double.parseDouble(numberStr);
} else if(Float.class.isAssignableFrom(clazz)) {
return Float.parseFloat(numberStr);
}
} catch(Exception e) {
return defaultValue;
}
return defaultValue;
}