数字格式+计算工具类

package util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

/**
* 数字格式+计算工具类
*/
public final class NumberUtil {

public NumberUtil() {
super();
}

public static final int HEX = 16;

public static final int DEICAML = 10;

/**
* 字符串 转换成 整数
*
* @param str
* @return
*/
public static int toInteger(String str) {
int result = 0;
try {
result = Integer.valueOf(str);
} catch (Exception e) {
result = 0;
}
return result;
}

/**
* 字符串 转换成 整数
*
* @param str
* @return
*/
public static int toInteger(Object str) {
if (str == null) {
return 0;
}
int result = 0;
try {
result = Integer.parseInt((String) str);
} catch (Exception e) {
result = 0;
}
return result;
}

public static Short toShort(Object str) {
if (str == null) {
return 0;
}
short result = 0;
try {
result = Short.valueOf((String) str);
} catch (Exception e) {
result = 0;
}
return result;
}

/**
* double to int
*
* @param dou
* @return
*/
public static Integer toDoubleToInterger(String str) {
int result = 0;
try {
double dou = Double.valueOf(str);
result = (int) dou;
} catch (Exception e) {
result = 0;
}
return result;
}

/**
* 字符串 转换成 Double
*
* @param str
* @return
*/
public static Double toDouble(String string) {
Double result = null;
try {
result = Double.valueOf(string);
} catch (Exception e) {
result = 0.0;
}
return result;
}

/**
* 字符串 转换成 Double
*
* @param str
* @return
*/
public static Double toDouble(String string, Double df) {
Double result = null;
try {
result = Double.valueOf(string);
} catch (Exception e) {
result = df;
}
return result;
}

/**
* 转换失败默认为0
*
* @param string
* @param radix
* @return
*/
public static int toInteger(String string, int radix) {
int result = 0;
try {
result = Integer.valueOf(string, radix);
} catch (Exception e) {
result = 0;
}
return result;
}

/**
* 转换失败默认0
*
* @param string
* @return
*/
public static float toFloat(String string) {
float result = 0;
try {
result = Float.valueOf(string);
} catch (Exception e) {
result = 0;
}
return result;
}

/**
* 转换失败默认为BigDecimal.ZERO
*
* @param string
* @return
*/
public static BigDecimal toBigDecimal(String string) {
try {
return new BigDecimal(string);
} catch (Exception e) {
return BigDecimal.ZERO;
}
}

/**
* 转换失败默认0
*
* @param string
* @return
*/
public static long toLong(String string) {
long result = 0;
try {
result = Long.valueOf(string);
} catch (Exception e) {
result = 0;
}
return result;
}

/**
* 转换失败默认0
*
* @param string
* @return
*/
public static long toLong(Object string) {
if (string == null) {
return 0;
}
long result = 0;

try {
String str = (String) string;
result = Long.valueOf(str);
} catch (Exception e) {
result = 0;
}
return result;
}

/**
* 数据显示格式化,如果number为null,默认按照0处理
*
* @return
*/
public static String numberFormat(Number number, String format) {
DecimalFormat decimalFormat = new DecimalFormat(format);
if (number != null) {
return decimalFormat.format(number);
} else {
return decimalFormat.format(0);
}
}

/**
* BigDecimal类型数据相加
*
* @param bd1
* @param bd2
* @return
*/
public static BigDecimal add(BigDecimal... bds) {
BigDecimal rt = null;
for (int i = 0; i < bds.length; i++) {
if (bds[i] != null) {
if (rt == null) {
rt = bds[i];
} else {
rt = rt.add(bds[i]);
}
}
}
return rt;
}

/**
* BigDecimal类型数据相减
*
* @param bd1
* @param bd2
* @return
*/
public static BigDecimal subtract(BigDecimal... bds) {
BigDecimal rt = null;
for (int i = 0; i < bds.length; i++) {
if (bds[i] != null) {
if (rt == null) {
rt = bds[i];
} else {
rt = rt.subtract((bds[i]));
}
}
}
return rt;
}

/**
* 年利率转换成日利率,保留6位有效小数
*
* @param yearIntRate
* @return
*/
public static BigDecimal yeart2dayIntRate(BigDecimal yearIntRate) {
if (yearIntRate == null) {
return null;
}
return yearIntRate.divide(new BigDecimal("360"), 6, RoundingMode.UP);
}

/**
* BigDecimal大小比较 <li>bigDecimalCompareTo(null,null)==0</li> <li>
* bigDecimalCompareTo(null,value)==-1</li> <li>bigDecimalCompareTo(value,null)==1</li> <li>
* bigDecimalCompareTo(value1,value2)==value1.compareTo(value2)</li>
*
* @param value1
* @param value2
* @return
*/
public static int bigDecimalCompareTo(BigDecimal value1, BigDecimal value2) {
BigDecimal first = value1 == null ? BigDecimal.ZERO : value1;
BigDecimal second = value2 == null ? BigDecimal.ZERO : value2;
return first.compareTo(second);
}

/**
* double sum
*
* @param list
* @return
*/
public static Double doubleSum(List<Double> list) {
Double doubleSum = 0.0;
if (list == null) {
return null;
}
for (Double dou : list) {
if (dou != null) {
doubleSum += dou;
}
}
return doubleSum;
}

/**
* double max
*
* @param list
* @return
*/
public static Double doubleMax(List<Double> list) {
Double doubleMax = 0.0;
if (list == null) {
return null;
}
for (Double dou : list) {
if (dou != null) {
if (doubleMax.compareTo(dou) < 0) {
doubleMax = dou;
}
}
}
return doubleMax;
}

public static BigDecimal toDecimal(Object obj) {
if (obj == null) {
return null;
} else {
BigDecimal result;
try {
result = new BigDecimal(obj.toString());
return result;
} catch (RuntimeException e) {
return null;
}
}
}

/**
* 将小数转化为百分数,保留2位小数
* //例:0.00218622829424932 ——> 0.22%
*
* @param df
* @return
*/
public static String toPercent(Double df) {
if (df == null) {
return null;
}
NumberFormat nt = NumberFormat.getPercentInstance();
nt.setMinimumFractionDigits(2);
return nt.format(df);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值