update ruoyi-common/src/main/java/com/ruoyi/common/utils/Arith.java.

duhanyu
phper08 4 years ago
parent 62081aebb9
commit 7d0f5e94ef

@ -1,114 +1,114 @@
package com.ruoyi.common.utils; package com.ruoyi.common.utils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
/** /**
* *
* *
* @author ruoyi * @author ruoyi
*/ */
public class Arith public class Arith
{ {
/** 默认除法运算精度 */ /** 默认除法运算精度 */
private static final int DEF_DIV_SCALE = 10; private static final int DEF_DIV_SCALE = 10;
/** 这个类不能实例化 */ /** 这个类不能实例化 */
private Arith() private Arith()
{ {
} }
/** /**
* *
* @param v1 * @param v1
* @param v2 * @param v2
* @return * @return
*/ */
public static double add(double v1, double v2) public static double add(double v1, double v2)
{ {
BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2)); BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue(); return b1.add(b2).doubleValue();
} }
/** /**
* *
* @param v1 * @param v1
* @param v2 * @param v2
* @return * @return
*/ */
public static double sub(double v1, double v2) public static double sub(double v1, double v2)
{ {
BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2)); BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue(); return b1.subtract(b2).doubleValue();
} }
/** /**
* *
* @param v1 * @param v1
* @param v2 * @param v2
* @return * @return
*/ */
public static double mul(double v1, double v2) public static double mul(double v1, double v2)
{ {
BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2)); BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue(); return b1.multiply(b2).doubleValue();
} }
/** /**
* *
* 10 * 10
* @param v1 * @param v1
* @param v2 * @param v2
* @return * @return
*/ */
public static double div(double v1, double v2) public static double div(double v1, double v2)
{ {
return div(v1, v2, DEF_DIV_SCALE); return div(v1, v2, DEF_DIV_SCALE);
} }
/** /**
* scale * scale
* *
* @param v1 * @param v1
* @param v2 * @param v2
* @param scale * @param scale
* @return * @return
*/ */
public static double div(double v1, double v2, int scale) public static double div(double v1, double v2, int scale)
{ {
if (scale < 0) if (scale < 0)
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"The scale must be a positive integer or zero"); "The scale must be a positive integer or zero");
} }
BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2)); BigDecimal b2 = new BigDecimal(Double.toString(v2));
if (b1.compareTo(BigDecimal.ZERO) == 0) if (b1.compareTo(BigDecimal.ZERO) == 0)
{ {
return BigDecimal.ZERO.doubleValue(); return BigDecimal.ZERO.doubleValue();
} }
return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue(); return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
} }
/** /**
* *
* @param v * @param v
* @param scale * @param scale
* @return * @return
*/ */
public static double round(double v, int scale) public static double round(double v, int scale)
{ {
if (scale < 0) if (scale < 0)
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"The scale must be a positive integer or zero"); "The scale must be a positive integer or zero");
} }
BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1"); BigDecimal one = BigDecimal.ONE;
return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue(); return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
} }
} }

Loading…
Cancel
Save