diff --git a/src/main/java/aiyh/utils/AmountHandler.java b/src/main/java/aiyh/utils/AmountHandler.java new file mode 100644 index 0000000..0ccdab0 --- /dev/null +++ b/src/main/java/aiyh/utils/AmountHandler.java @@ -0,0 +1,178 @@ +package aiyh.utils; + +/** + * @Author DYS + * @Date 2023/3/25 18:08 + * @description create at weaver + */ + +import java.io.Serializable; +import java.math.BigDecimal; + +/** + * 金额类型. 支持自身的四则运算,并将this返回. + */ +public class AmountHandler implements Serializable { + + private BigDecimal value; + + /** + * 提供默认精度10 + */ + private int scale = 10; + + public AmountHandler() { + + } + + /** + * double类型构造函数 + * + * @param value + */ + public AmountHandler(double value) { + this.value = new BigDecimal(Double.toString(value)); + } + + /** + * String类型构造函数 + * + * @param value + */ + public AmountHandler(String value) { + this.value = new BigDecimal(value); + } + + /** + * 取得BigDecimal的值 + * + * @return + */ + public BigDecimal getValue() { + return this.value; + } + + /** + * 两个double类型的数值相加 + * + * @param v1 + * @param v2 + * @return + */ + public double add(double v1, double v2) { + AmountHandler a1 = new AmountHandler(v1); + AmountHandler a2 = new AmountHandler(v2); + return add(a1, a2); + } + + /** + * 两数相除 + * + * @param v1 + * @param v2 + * @return + */ + public double div(double v1, double v2) { + AmountHandler a1 = new AmountHandler(v1); + AmountHandler a2 = new AmountHandler(v2); + return this.divide(a1, a2); + } + + /** + * 相减 + * + * @param v1 + * @param v2 + * @return + */ + public double sub(double v1, double v2) { + AmountHandler a1 = new AmountHandler(v1); + AmountHandler a2 = new AmountHandler(v2); + return this.subtract(a1, a2); + } + + /** + * 相乘 + * + * @param v1 + * @param v2 + * @return + */ + public double mul(double v1, double v2) { + AmountHandler a1 = new AmountHandler(v1); + AmountHandler a2 = new AmountHandler(v2); + return this.multiply(a1, a2); + } + + /** + * 两个Amount类型的数据进行相加 + * + * @param v1 + * @param v2 + * @return + */ + public double add(AmountHandler v1, AmountHandler v2) { + return v1.getValue().add(v2.getValue()).doubleValue(); + } + + /** + * 两个Amount类型变量相除 + * + * @param v1 + * @param v2 + * @return + */ + public double divide(AmountHandler v1, AmountHandler v2) { + if (scale < 0) { + throw new IllegalArgumentException("精度指定错误,请指定一个>=0的精度"); + } + return v1.getValue().divide(v2.getValue(), scale, + BigDecimal.ROUND_HALF_UP).doubleValue(); + } + + /** + * 两数相乘 + * + * @param v1 + * @param v2 + * @return + */ + public double multiply(AmountHandler v1, AmountHandler v2) { + return v1.getValue().multiply(v2.getValue()).doubleValue(); + } + + /** + * 两数相减 + * + * @param v1 + * @param v2 + * @return + */ + public double subtract(AmountHandler v1, AmountHandler v2) { + return v1.getValue().subtract(v2.getValue()).doubleValue(); + } + + /** + * 返回value的浮点数值 + * + * @return + */ + public double doubleValue() { + return this.getValue().doubleValue(); + } + + /** + * 设置精度 + * + * @param scale + */ + public void setScale(int scale) { + this.scale = scale; + } + + public static void main(String[] args) { + AmountHandler AmountHandler = new AmountHandler(5000.23); + AmountHandler AmountHandler1 = new AmountHandler(2033.37); + System.out.println(new AmountHandler().add(AmountHandler, AmountHandler1)); + } +}