/**
* 数字工具类
*
* @author wangda_cn@126.com
*/
public class NumberUtils extends org.apache.commons.lang3.math.NumberUtils{
/**
* 将一个对象转换为long类型
* @param obj
* @return 如果转换失败,返回Long.MIN_VALUE
*/
public static long toLong(Object obj) {
return toLong(obj, Long.MIN_VALUE);
}
/**
* 将一个对象转换为long类型
* @param obj
* @param def 转换失败返回的值
* @return
*/
public static long toLong(Object obj, long def) {
if (obj == null)
return 0;
String name = obj.getClass().getSimpleName().toLowerCase();
if ("string".equals(name)) {
try {
long l = Long.parseLong((String)obj);
return l;
}
catch(Exception ex) {
return def;
}
}
else if ("long".equals(name)) {
return (Long)obj;
}
else if ("bigdecimal".equals(name)) {
return ((BigDecimal)obj).longValue();
}
else if ("double".equals(name)) {
return ((Double)obj).longValue();
}
else if ("integer".equals(name)) {
return (Integer)obj;
}
else if ("biginteger".equals(name)) {
return ((BigInteger)obj).longValue();
}
return def;
}
/**
* 将一个对象转换为double类型
* @param obj
* @return 转换失败后,返回Double.MIN_VALUE
*/
public static double toDouble(Object obj) {
return toDouble(obj, Double.MIN_VALUE);
}
/**
* 将一个对象转换为double类型
* @param obj
* @param def
* @return
*/
public static double toDouble(Object obj, double def) {
if (obj == null)
return 0d;
String name = obj.getClass().getSimpleName().toLowerCase();
if ("string".equals(name)) {
try {
double d = Double.parseDouble((String)obj);
return d;
}
catch(Exception ex) {
return def;
}
}
else if ("double".equals(name)) {
return (Double)obj;
}
else if ("bigdecimal".equals(name)) {
return ((BigDecimal)obj).doubleValue();
}
else if("long".equals(name)) {
return ((Long)obj).doubleValue();
}
else if ("integer".equals(name)) {
return ((Integer)obj).doubleValue();
}
else if ("biginteger".equals(name)) {
return ((BigInteger)obj).doubleValue();
}
return def;
}
/**
* 将一个对象转换为int类型
* @param obj
* @return
*/
public static int toInt(Object obj) {
return toInt(obj, Integer.MIN_VALUE);
}
/**
* 将一个对象转换BigDecimal
* @param val 需要转换的对象
* @param def 默认值
* @return 转换后的值
*/
public static BigDecimal toDecimal(Object val, BigDecimal def) {
try {
BigDecimal decimal = TypeUtils.toBigDecimal(val);
return decimal == null? def: decimal;
} catch(Exception ex) {
return def;
}
}
/**
* 将一个对象转换为int类型
* @param obj
* @param def
* @return
*/
public static int toInt(Object obj, int def) {
if (obj == null)
return def;
String name = obj.getClass().getSimpleName().toLowerCase();
if ("string".equals(name)) {
try {
int i = Integer.parseInt((String) obj);
return i;
}
catch(Exception ex) {
return def;
}
}
else if ("long".equals(name)) {
return ((Long)obj).intValue();
}
else if ("integer".equals(name)) {
return (Integer)obj;
}
else if ("bigdecimal".equals(name)) {
return ((BigDecimal)obj).intValue();
}
else if ("double".equals(name)) {
return ((Double)obj).intValue();
}
else if ("biginteger".equals(name)) {
return ((BigInteger)obj).intValue();
}
return def;
}
/**
* 将一个double截断小数位
* @param d
* @param digit 保留位数
* @return
*/
public static double round(double d, int digit) {
int n = 1;
for (int i=0; i<digit; i++) {
n *= 10;
}
return ((double)Math.round(d*n))/n;
}
/**
* 两个浮点数相除
* @param d1
* @param d2
* @return
*/
public static double division(double d1, double d2) {
BigDecimal bd1 = new BigDecimal(d1 + "");
BigDecimal bd2 = new BigDecimal(d2 + "");
BigDecimal result = bd1.divide(bd2);
return result.doubleValue();
}
/**
* 格式化一个数字
* @param num 要格式化的数字
* @param fmt 格式
* @return
*/
public static String format(double num, String fmt) {
DecimalFormat formatter = new DecimalFormat(fmt);
return formatter.format(num);
}
/**
* 生成一个随机整数
* @param maxNumber 最大数
* @return
*/
public static int randInt(int maxNumber) {
Random rand = new Random();
return rand.nextInt(maxNumber);
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
NumberUtils.toDouble("1.2");
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end-start) + " ms");
start = System.currentTimeMillis();
for(int i=0; i<1000000; i++) {
Double.parseDouble("1.2");
}
end = System.currentTimeMillis();
System.out.println("耗时:" + (end-start) + " ms");
System.out.println(NumberUtils.round(1.23456, 2));
}
}