public class CalculationUtil {
/**
* 元转分进率
*/
public static final BigDecimal YUAN_TRANSFER_RATE = new BigDecimal(100);
/**
* 类型转换
* 任意类型转为 BigDecimal
*
* @param text
* @return
*/
private static BigDecimal typeConversion(@NonNull Object text) throws Exception {
BigDecimal text_1 = null;
if (text instanceof Integer) {
int value = ((Integer) text).intValue();
text_1 = new BigDecimal(value);
} else if (text instanceof String) {
String s = (String) text;
if (isInteger(s) || isDouble(s)) {
text_1 = new BigDecimal(s);
} else {
throw new Exception("传入数值不对,需传入数字或小数");
}
} else if (text instanceof Double) {
double d = ((Double) text).doubleValue();
text_1 = new BigDecimal(d);
} else if (text instanceof Float) {
float f = ((Float) text).floatValue();
text_1 = new BigDecimal(f);
} else if (text instanceof Long) {
long l = ((Long) text).longValue();
text_1 = new BigDecimal(l);
} else if (text instanceof Boolean) {
boolean b = ((Boolean) text).booleanValue();
throw new Exception("传入数值不对,需传入数字或小数");
} else if (text instanceof Date) {
Date d = (Date) text;
throw new Exception("传入数值不对,需传入数字或小数");
} else {
text_1 = (BigDecimal) text;
}
return text_1;
}
//判断整数(int)
public static boolean isInteger(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
//判断浮点数(double和float)
public static boolean isDouble(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("[+-]?[0-9]+\\.([0-9]+)"); // 之前这里正则表达式错误,现更正
return pattern.matcher(str).matches();
}
/**
* 使用java正则表达式去掉多余的.与0
*
* @param s
* @return string
*/
public static BigDecimal replace(@NonNull String s) {
if (null != s && s.indexOf(".") > 0) {
s = s.replaceAll("0+?$", "");//去掉多余的0
s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
}
return new BigDecimal(s);
}
/**
* 元转分
*
* @param text
* @return
*/
public static BigDecimal yuanToPoints(@NonNull Object text) {
BigDecimal yuan = null;
try {
yuan = typeConversion(text);
} catch (Exception e) {
e.printStackTrace();
return yuan;
}
//元转分计算 除以以进率
return replace(rounding(yuan).multiply(YUAN_TRANSFER_RATE).toString());
}
/**
* 分转元
*
* @param text
* @return
*/
public static BigDecimal centsToYuan(@NonNull Object text) {
BigDecimal fen = null;
try {
fen = typeConversion(text);
} catch (Exception e) {
e.printStackTrace();
return fen;
}
//分转元计算 乘以进率
return replace(fen.divide(YUAN_TRANSFER_RATE).toString());
}
/**
* BigDecimal四舍五入保留两位小数
*
* @param bigDecimal
* @return
*/
public static BigDecimal rounding(@NonNull BigDecimal bigDecimal) {
return bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
}
/**
* 判断字符串是否为数字(正整数和浮点数)
*
* @param str
* @return
*/
public static boolean isNumeric(String str) {
String reg = "^[0-9]+(.[0-9]+)?$";
Pattern pattern = Pattern.compile(reg);
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
// public static void main(String[] args) {
// String a = "111.238";
// BigDecimal bigDecimal = new BigDecimal(20000);
// BigDecimal b = yuanToPoints(bigDecimal);
// System.out.println(b.toString());
// System.out.println(rounding(b));
// System.out.println(replace(rounding(b).toString()));
// }
}
Java 金额计算工具类 元转分 分转元
最新推荐文章于 2024-08-01 04:44:53 发布