package numTest;
import java.math.BigDecimal;
/**
* 高精度计算辅助类,提供保留指定小数位的简单计算和四舍五入
*/
public class Num {
private static final BigDecimal one = new BigDecimal(1);
private BigDecimal decimal;
private int scale;
private int roundingMode = BigDecimal.ROUND_HALF_UP;
public Num() {
decimal = new BigDecimal(0);
}
public Num(double val) {
this.decimal = new BigDecimal(val);
}
/* public Num(double val, int scale) {
this.scale = scale;
this.decimal = new BigDecimal(val).setScale(scale, roundingMode);
}*/
public Num(double val, int scale, int roundingMode) {
this.scale = scale;
this.roundingMode = roundingMode;
this.decimal = new BigDecimal(val).setScale(scale, roundingMode);
}
public Num(Num num, int scale, int roundingMode) {
decimal = num.decimal.setScale(scale, roundingMode);
}
public BigDecimal getDecimal() {
return decimal;
}
public Num setDecimal(BigDecimal decimal) {
this.decimal = decimal;
return this;
}
public int getScale() {
return scale;
}
public void setScale(int scale) {
this.scale = scale;
this.decimal = this.decimal.setScale(scale);
}
public double doubleValue() {
return decimal.doubleValue();
}
public Num divide(Num num) {
return divide(setOtherNum(num), scale, roundingMode);
}
public Num divide(double val) {
Num num = new Num(val);
return divide(num);
}
public Num divide(Num num, int scale) {
return divide(num, scale, roundingMode);
}
public Num divide(Num num, int scale, int roundingMode) {
return setDecimal(this.getDecimal().divide(num.getDecimal(), scale, roundingMode));
}
public Num multiply(Num num) {
return multiply(setOtherNum(num), scale, roundingMode);
}
public Num multiply(double val) {
Num num = new Num(val);
return multiply(num);
}
public Num multiply(Num num, int scale) {
return multiply(num, scale, roundingMode);
}
public Num multiply(Num num, int scale, int roundingMode) {
return setDecimal(rounding(this.getDecimal().multiply(num.getDecimal()), scale, roundingMode));
}
public Num add(Num num) {
return add(setOtherNum(num), scale, roundingMode);
}
public Num add(double val) {
Num num = new Num(val);
return add(num);
}
public Num add(Num num, int scale) {
return add(num, scale, roundingMode);
}
public Num add(Num num, int scale, int roundingMode) {
return setDecimal(rounding(this.getDecimal().add(num.getDecimal()), scale, roundingMode));
}
public Num sub(Num num) {
return sub(setOtherNum(num), scale);
}
public Num sub(double val) {
Num num = new Num(val);
return sub(num);
}
public Num sub(Num num, int scale) {
return sub(num, scale, roundingMode);
}
public Num sub(Num num, int scale, int roundingMode) {
BigDecimal result = this.getDecimal().subtract(num.getDecimal());
result = rounding(result, scale, roundingMode);
return setDecimal(result);
}
private Num setOtherNum(Num num) {
num = new Num(num, scale, roundingMode);
return num;
}
public double round(int scale) {
return rounding(this.decimal, scale, roundingMode).doubleValue();
}
public double round(int scale, int roundingMode) {
return rounding(this.decimal, scale, roundingMode).doubleValue();
}
public static BigDecimal rounding(BigDecimal decimal, int scale, int roundingMode) {
return decimal.divide(one, scale, roundingMode);
}
public static BigDecimal rounding(BigDecimal decimal, int scale) {
return rounding(decimal, scale, BigDecimal.ROUND_HALF_UP);
}
public static double rounding(double val, int scale) {
return rounding(val, scale, BigDecimal.ROUND_HALF_UP);
}
public static double rounding(double val, int scale, int roundingMode) {
return rounding(new BigDecimal(val), scale, roundingMode).doubleValue();
}
@Override
public String toString() {
return "Num [decimal=" + decimal + ", scale=" + scale + ", roundingMode=" + roundingMode + "]";
}
}