金额类型字段 + - * / 处理

main
yushun.ding 2023-03-27 13:03:45 +08:00
parent 3c49ade7b4
commit ee64140219
1 changed files with 178 additions and 0 deletions

View File

@ -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));
}
}