big
public Double divSum(Double a, Double b) {
if (a != null && b != null && a > 0 && b > 0) {
return BigDecimal.valueOf(a).divide(BigDecimal.valueOf(b))
.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
return 0.0;
}
public Double rateNum(Double a, Double b) {
if (a != null && b != null && a > 0 && b > 0) {
Double c = divSum(a, 100.0);
return mulSum(c, b);
}
return 0.0;
}
public Double mulSum(Double a, Double b) {
if (a != null && b != null && a > 0 && b > 0) {
return BigDecimal.valueOf(a).multiply(BigDecimal.valueOf(b))
.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
return 0.0;
}
最新:
public Double deductSum(Double[] deducts) {
if (deducts != null && deducts.length > 0) {
BigDecimal sum = BigDecimal.valueOf(0.0);
if (deducts.length == 1) {
if (deducts[0] != null) {
sum.add(BigDecimal.valueOf(deducts[0]));
} else {
return 0.0;
}
} else {
for (Double deduct : deducts) {
if (deduct != null) {
sum.add(BigDecimal.valueOf(deduct));
}
}
}
return sum.doubleValue();
}
return 0.0;
}
// a > 0 &&
public Double realSum(Double a, Double[] deducts) {
if (a != null && deducts != null && deducts.length > 0) {
BigDecimal sum = BigDecimal.valueOf(a);
if (deducts.length == 1) {
if (deducts[0] != null) {
sum.subtract(BigDecimal.valueOf(deducts[0]));
} else {
return a;
}
} else {
for (Double deduct : deducts) {
if (deduct != null) {
sum.subtract(BigDecimal.valueOf(deduct));
}
}
}
return sum.doubleValue();
}
return 0.0;
}
第二旧例子:
public Double deductSum(Double[] deducts) {
if (deducts != null && deducts.length > 0) {
BigDecimal sum = BigDecimal.valueOf(0.0);
if (deducts.length == 1) {
if (deducts[0] != null && deducts[0] > 0) {
sum.add(BigDecimal.valueOf(deducts[0]));
} else {
return 0.0;
}
} else {
for (Double deduct : deducts) {
if (deduct != null && deduct > 0) {
sum.add(BigDecimal.valueOf(deduct));
}
}
}
return sum.doubleValue();
}
return 0.0;
}
public Double realSum(Double a, Double[] deducts) {
if (a != null && a > 0 && deducts != null && deducts.length > 0) {
BigDecimal sum = BigDecimal.valueOf(a);
if (deducts.length == 1) {
if (deducts[0] != null && deducts[0] > 0) {
sum.subtract(BigDecimal.valueOf(deducts[0]));
} else {
return a;
}
} else {
for (Double deduct : deducts) {
if (deduct != null && deduct > 0) {
sum.subtract(BigDecimal.valueOf(deduct));
}
}
}
return sum.doubleValue();
}
return 0.0;
}
旧例子:
public Double realSum(Double a, Double[] deducts) {
if (a != null && a > 0 && deducts != null && deducts.length > 0) {
BigDecimal sum = BigDecimal.valueOf(a);
if (deducts.length == 1 && deducts[0] != null && deducts[0] > 0) {
sum.divide(BigDecimal.valueOf(deducts[0]));
} else {
for (Double deduct : deducts) {
if (deduct != null && deduct > 0) {
sum.subtract(BigDecimal.valueOf(deduct));
}
}
}
return sum.doubleValue();
}
return 0.0;
}
import java.math.BigDecimal;
BigDecimal allocation = BigDecimal.valueOf(allVo.getStudentAllocation());
BigDecimal signUp = BigDecimal.valueOf(allocationVo.getStudentSignUp());
BigDecimal divide = allocation.divide(signUp, 2, BigDecimal.ROUND_HALF_UP);
allocationVo.setStudentRate(divide.doubleValue()+"%");
public String divide(Integer all, Integer sign) {
if (all > 0 || sign > 0) {
BigDecimal allocation = BigDecimal.valueOf(all);
BigDecimal signUp = BigDecimal.valueOf(sign);
BigDecimal divide = signUp.divide(allocation, 2, BigDecimal.ROUND_HALF_UP);
return divide.doubleValue() + "%";
}
return "0%";
}
package cn.itcast_02;
import java.math.BigDecimal;
/*
* 构造方法:
* public BigDecimal(String val):
*
* 金融相关的东西(例:银行钱的小数,商品价格小数,实验小数)
*
* 成员方法:
* public BigDecimal add(BigDecimal augend):加
* public BigDecimal subtract(BigDecimal subtrahend):减
* public BigDecimal multiply(BigDecimal multiplicand):乘
* public BigDecimal divide(BigDecimal divisor):除
* public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取。
*
*/
public class BigDecimalDemo {
public static void main(String[] args) {
System.out.println(0.09 + 0.01);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);
// public BigDecimal add(BigDecimal augend):加
BigDecimal bd1 = new BigDecimal("0.09");
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println("add:" + bd1.add(bd2));
System.out.println("----------------------");
// public BigDecimal subtract(BigDecimal subtrahend):减
BigDecimal bd3 = new BigDecimal("1.0");
BigDecimal bd4 = new BigDecimal("0.32");
System.out.println("subtract:" + bd3.subtract(bd4));
System.out.println("----------------------");
// public BigDecimal multiply(BigDecimal multiplicand):乘
BigDecimal bd5 = new BigDecimal("1.015");
BigDecimal bd6 = new BigDecimal("100");
System.out.println("multiply:" + bd5.multiply(bd6));
System.out.println("----------------------");
// public BigDecimal divide(BigDecimal divisor):除
BigDecimal bd7 = new BigDecimal("1.301");
BigDecimal bd8 = new BigDecimal("100");
System.out.println("divide:" + bd7.divide(bd8));
System.out.println("divide:" + bd7.divide(bd8,3,BigDecimal.ROUND_HALF_UP));
System.out.println("divide:" + bd7.divide(bd8,9,BigDecimal.ROUND_HALF_UP));
}
}