import java.math.BigDecimal;
public class CalculationUtil {
/**
* 精确的加法运算
*
* @param value1 加数1
* @param value2 加数2
* @return
*/
public static String add(String value1, String value2) {
double a = 0;
double b = 0;
try {
a = Double.parseDouble(value1);
} catch (NumberFormatException | NullPointerException ignored) {
}
try {
b = Double.parseDouble(value2);
} catch (NumberFormatException | NullPointerException ignored) {
}
return add(a, b);
}
public static String add(double value1, double value2) {
return (BigDecimal.valueOf(value1).add(BigDecimal.valueOf(value2))).toString();
}
/**
* 精确的减法运算
*
* @param value1 被减数
* @param value2 减数
* @return
*/
public static String subtract(String value1, String value2) {
double a = 0;
double b = 0;
try {
a = Double.parseDouble(value1);
} catch (NumberFormatException | NullPointerException ignored) {
}
try {
b = Double.parseDouble(value2);
} catch (NumberFormatException | NullPointerException ignored) {
}
return subtract(a, b);
}
public static String subtract(double value1, double value2) {
return BigDecimal.valueOf(value1).subtract(BigDecimal.valueOf(value2)).toString();
}
/**
* 精确的乘法运算
*
* @param value1 乘数
* @param value2 乘数
* @return
*/
public static String multiply(String value1, String value2) {
double a = 0;
double b = 0;
try {
a = Double.parseDouble(value1);
} catch (NumberFormatException | NullPointerException ignored) {
}
try {
b = Double.parseDouble(value2);
} catch (NumberFormatException | NullPointerException ignored) {
}
return multiply(a, b);
}
public static String multiply(double value1, double value2) {
return BigDecimal.valueOf(value1).multiply(BigDecimal.valueOf(value2)).toString();
}
/**
* 精确的除法运算
*
* @param value1 被除数
* @param value2 除数
* @param scale 精确到小数点后几位数
* @param mode 精确模式
* @return
*/
public static String divide(String value1, String value2, int scale, int mode) {
double a = 0;
double b = 0;
try {
a = Double.parseDouble(value1);
} catch (NumberFormatException | NullPointerException ignored) {
}
try {
b = Double.parseDouble(value2);
} catch (NumberFormatException | NullPointerException ignored) {
}
return divide(a, b, scale, mode);
}
//四舍五入模式
public static String divide(String value1, String value2, int scale) {
return divide(value1, value2, scale, BigDecimal.ROUND_HALF_UP);
}
public static String divide(double value1, double value2, int scale, int mode) {
// return divide(Double.toString(value1),Double.toString(value2),scale,mode);
return BigDecimal.valueOf(value1).divide(BigDecimal.valueOf(value2), scale, mode).toString();
}
//四舍五入模式
public static String divide(double value1, double value2, int scale) {
return divide(value1, value2, scale, BigDecimal.ROUND_HALF_UP);
}
/**
* 测量(精确取舍)
*
* @param value 需要取舍的小数
* @param scale 小数点后几位
* @param mode 取舍模式
* @return
*/
public static String setScale(double value, int scale, int mode) {
return BigDecimal.valueOf(value).setScale(scale, mode).toString();
}
public static String setScale(String value, int scale, int mode) {
double v = 0;
try {
v = Double.parseDouble(value);
} catch (NumberFormatException | NullPointerException ignored) {
}
return setScale(v, scale, mode);
}
/**
* 测量(精确取舍) 四舍五入模式
*
* @param value 需要取舍的小数
* @param scale 小数点后几位
* @return
*/
public static String setScale(double value, int scale) {
return setScale(value, scale, BigDecimal.ROUND_HALF_UP);
}
/**
* 测量(精确取舍) 四舍五入模式
*
* @param value 需要取舍的小数
* @param scale 小数点后几位
* @return
*/
public static String setScale(String value, int scale) {
return setScale(value, scale, BigDecimal.ROUND_HALF_UP);
}
}