package CommonUtil;
import java.math.BigDecimal;
import java.util.regex.Pattern;
/**
* 处理基本数据类型转换的工具类 (字符串类型转换为BigDecimal金额、int、Double)
*/
public class DataTypeUtil {
/**
* 字符串转换为BigDecimal金额类型
*/
public static BigDecimal getBigDecimal(String s) {
if (s != null && Pattern.matches("^\\d+\\.??\\d+$", s)) {
try {
return new BigDecimal(s);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 字符串转换为int类型
*/
public static int parseInt(String s) {
if (s != null && Pattern.matches("^\\d*$", s)) {
try {
return Integer.parseInt(s);
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
}
/**
* 字符串转换为转换Double型
*/
public static Double parseDouble(String s) {
if (s != null && Pattern.matches(
"^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?[dD]?$", s)) {
try {
return Double.parseDouble(s);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return null;
}
}