package calc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
/**
* @Description 计算器
* @author Sunny
* @date 2018/8/2 0002
* @param
* @return
*/
public class CalculateDecimal {
private static final Logger logger = LoggerFactory.getLogger(CalculateDecimal.class);
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.00");
/**
* 对两个数进行四则运算; 对于除法的运算,保留4位小数;
* result = num1+num2;
* result = num1-num2;
* result = num1*num2;
* result = num1/num2;
* @param num1 第一个数;
* @param num2 第二个数;
* @param calcSymbol 运算符号 "+" "-" "*" "/"
* @return BigDecimal
* @throws Exception 计算异常;
*/
public static BigDecimal calcNumber(String num1, String num2, String calcSymbol) {
return calcNumber(num1, num2, calcSymbol,4);
}
public static BigDecimal calcNumber(Double num1, Double num2, String calcSymbol) {
return calcNumber(num1, num2, calcSymbol,4);
}
public static BigDecimal calcNumber(Long num1, Long num2, String calcSymbol) {
return calcNumber(num1, num2, calcSymbol,4);
}
public static BigDecimal calcNumber(int num1, int num2, String calcSymbol) {
return calcNumber(num1, num2, calcSymbol,4);
}
/**
* 对两个数进行四则运算; 自行选择保留位数;不四舍五入,直接截取;
* result = num1+num2;
* result = num1-num2;
* result = num1*num2;
* result = num1/num2;
* @param num1 第一个数;
* @param num2 第二个数;
* @param calcSymbol 运算符号 "+" "-" "*" "/"
* @param remainNum 保留多少位小数; 如果小于0,则为0;
* @return BigDecimal
* @throws Exception 计算异常;
*/
public static BigDecimal calcNumber(Object num1, Object num2, String calcSymbol,int remainNum) {
remainNum = remainNum<0?0:remainNum;
if (StringUtils.isEmpty(num1)||StringUtils.isEmpty(num2)) {
return null;
}
try {
BigDecimal decimal = new BigDecimal(num1.toString());
BigDecimal decima2 = new BigDecimal(num2.toString());
if ("+".equals(calcSymbol)) {
return decimal.add(decima2);
} else if ("-".equals(calcSymbol)) {
return decimal.subtract(decima2);
} else if ("*".equals(calcSymbol)) {
return decimal.multiply(decima2).setScale(remainNum, BigDecimal.ROUND_DOWN);
} else if ("/".equals(calcSymbol)) {
if (!num2.equals("0")) {
return decimal.divide(decima2, remainNum,
BigDecimal.ROUND_DOWN);
}else{
throw new ArithmeticException("/ by zero");
}
}
} catch (Exception e) {
logger.error("计算出错",e);
}
return null;
}
/**
* 对两个数进行四则运算; 自行选择保留位数;四舍五入
* result = num1+num2;
* result = num1-num2;
* result = num1*num2;
* result = num1/num2;
*/
public static BigDecimal calcNumberHalfDown(Object num1, Object num2, String calcSymbol,int remainNum) {
remainNum = remainNum<0?0:remainNum;
if (StringUtils.isEmpty(num1)||StringUtils.isEmpty(num2)) {
return null;
}
try {
BigDecimal decimal = new BigDecimal(num1.toString());
BigDecimal decima2 = new BigDecimal(num2.toString());
if ("+".equals(calcSymbol)) {
return decimal.add(decima2);
} else if ("-".equals(calcSymbol)) {
return decimal.subtract(decima2);
} else if ("*".equals(calcSymbol)) {
return decimal.multiply(decima2).setScale(remainNum, BigDecimal.ROUND_HALF_DOWN);
} else if ("/".equals(calcSymbol)) {
if (!num2.equals("0")) {
return decimal.divide(decima2, remainNum,
BigDecimal.ROUND_HALF_DOWN);
}else{
throw new ArithmeticException("/ by zero");
}
}
} catch (Exception e) {
logger.error("计算出错",e);
}
return null;
}
public static String numberFormat(String source) {
try {
return DECIMAL_FORMAT.format(Double.valueOf(source));
} catch (NumberFormatException e) {
logger.error("##数字格式化出错{}##",e);
}
return source+"";
}
public static String numberFormat(double source) {
try {
return DECIMAL_FORMAT.format(source);
} catch (NumberFormatException e) {
logger.error("##数字格式化出错{}##",e);
}
return source+"";
}
/**
* 将分为单位的钱格式化成元为单位(保留两位小数)
* @param source
* @return
*/
public static String numberFormatCoin(double source) {
BigDecimal money = calcNumber(source, 100, "/", 2);
return numberFormat(money.doubleValue());
}
/**
* 将分为单位的钱格式化成元为单位(取整数)
* @param source
* @return
*/
public static String numberFormatInt(double source) {
return calcNumber(source, 0.01, "*", 2).intValue() + "";
}
/**
* 将分为单位的钱格式化成元为单位(四舍五入 )
* @param source
* @return
*/
public static String numberFormatIntHalfDown(double source) {
return calcNumberHalfDown(source, 100, "/", 0).intValue() + "";
}
/**
* 将分为单位的金额抹去小数点
* @param source
* @return
*/
public static double numberDownCoin(double source) {
BigDecimal money = new BigDecimal(source);
return money.setScale(0, BigDecimal.ROUND_DOWN).doubleValue();
}
/**
* 金额转化为大写
* @param money
* @return
*/
public static String getCnMoney(Double money){
final String UNIT = "万仟佰拾亿仟佰拾万仟佰拾元角分";
final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
final double MAX_VALUE = 9999999999999.99D;
if (money < 0 || money > MAX_VALUE) {
return "参数非法!";
}
long l = Math.round(money * 100);
if (l == 0) {
return "零元整";
}
String strValue = l + "";
// i用来控制数
int i = 0;
// j用来控制单位
int j = UNIT.length() - strValue.length();
String rs = "";
boolean isZero = false;
for (; i < strValue.length(); i++, j++) {
char ch = strValue.charAt(i);
if (ch == '0') {
isZero = true;
if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万'
|| UNIT.charAt(j) == '元') {
rs = rs + UNIT.charAt(j);
isZero = false;
}
} else {
if (isZero) {
rs = rs + "零";
isZero = false;
}
rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
}
}
rs = rs.replaceAll("亿万", "亿");
return rs;
}
public static void main(String[] args) {
double testdf=0.000052;
System.out.println(numberFormat(testdf));
double strMul=6.0000615;
System.out.println(numberFormat(strMul));
double bdfScale=0.0078123457;
System.out.println(numberFormat(bdfScale));
double dbNumberF=1.234567891238;
System.out.println(numberFormat(dbNumberF));
String dbNumber="123456789.124";
System.out.println(numberFormat(dbNumber));
String cnMoney = getCnMoney(11002358.01);
System.out.println(cnMoney);
}
}
打印结果如下:
0.00
6.00
0.01
1.23
123456789.12
壹仟壹佰万贰仟叁佰伍拾捌元零壹分