public class NumberHelper {
/*
*是否数字
*/
public final static boolean isNumeric(String s) {
if (s != null && !"".equals(s.trim()))
return s.matches("^[0-9]*$");
else
return false;
}
/**
* 生成整形随机数
* @param min
* @param max
*/
public static int intRandom(int min, int max){
max += 1;
int result = min + ((int)(new Random().nextFloat() * (max - min)));
return result;
}
/**
* 字符串转换成INT
* @param str
* @param defaultVal
* @return
*/
public static Integer stringToInt(String str, Integer defaultVal){
Double d = stringToDouble(str, 0d);
if(d.equals(0d)){
return defaultVal;
}
return d.intValue();
}
/**
* 字符串转长整形
* @param str
* @param defaultVal
* @return
*/
public static Long stringToLong(String str, Long defaultVal){
Double d = stringToDouble(str, 0d);
if(d.equals(0d)){
return defaultVal;
}
return d.longValue();
/**
* 格式化DO
* @param val
* @param decimalN
* @return
*/
public static Double formatDouble(Double val, int decimalN){
if(val==null){
return 0d;
}
BigDecimal b = new BigDecimal(Double.toString(val));
return b.setScale(decimalN, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static Double stringToDouble(String odds, double d) {
if(odds == null){
return d;
}
try{
return Double.parseDouble(odds);
}catch(Exception ex){
return d;
}
}
public static Float stringToFloat(String odds, float f) {
if(odds == null){
return f;
}
try{
return Float.parseFloat(odds);
}catch(Exception ex){
return f;
}
}
}