1.例子
public class Test {
public static void main(String[] args) {
BigDecimal big1 = new BigDecimal("600");
BigDecimal big2 = new BigDecimal("300");
//两个BigDecimal相加
big1 = big1.add(big2);
System.out.println(big1);
//两个BigDecimal相减
big1 = big1.subtract(big2);
System.out.println(big1);
//两个BigDecimal相乘
big1 = big1.multiply(big2);
System.out.println(big1);
//两个BigDecimal相除
big1 = big1.divide(big2);
System.out.println(big1);
//如果是Long 型
BigDecimal big3 = BigDecimal.valueOf(200L); big1 = big1.subtract(big3); System.out.println("big1=" + big1);
}
}
//输出效果
BigDecimal的用法 - 賢賢 - ╉落淚的魚┽賢
2.项目中的应用
/**
* 1.降要删除的数据的paymentSummaryid设为null ;
* 2.总金额相减
*
* @param paymentId
*/
public void setPaymentSummaryIdNull(Long paymentId, Long paymentSummaryId) {
Payment payment = payMentRepository.findOne(paymentId);
PaymentSummary summary = paymentSummaryRepository.findOne(78456137l);
int count = findPaymentSummaryIdList(paymentSummaryId).size();//总数
List resultList = findPaymentSummaryIdList(paymentSummaryId);
if (count != 0) {
for (int i = 0; i < count; i++) {
if (resultList.get(i) == paymentId) { //如果list里的ID等于paymentId总数相减
PaymentSummary paymentSummary = paymentSummaryRepository.findOne(paymentSummaryId);
//paymentSummary.getSummaryFee()是BigDecimal类型,(paymentSummary是实体)
//必须得先把paymentSummary.getSummaryFee()转换成String 在转成Double
BigDecimal summaryFee = new BigDecimal
(Double.valueOf(paymentSummary.getSummaryFee().toString())); //得到总金额
BigDecimal paymentFee = new BigDecimal
(Double.valueOf(payment.getFee().toString())); //得到要修改的数据的费用
paymentSummary.setSummaryFee(summaryFee.subtract(paymentFee));//两个值相减
paymentSummary = paymentSummaryRepository.save(paymentSummary);
}
}
}
payment.getFee();
payment.setPaymentSummary(summary);
payment = payMentRepository.save(payment);
}
public class Test {
public static void main(String[] args) {
BigDecimal big1 = new BigDecimal("600");
BigDecimal big2 = new BigDecimal("300");
//两个BigDecimal相加
big1 = big1.add(big2);
System.out.println(big1);
//两个BigDecimal相减
big1 = big1.subtract(big2);
System.out.println(big1);
//两个BigDecimal相乘
big1 = big1.multiply(big2);
System.out.println(big1);
//两个BigDecimal相除
big1 = big1.divide(big2);
System.out.println(big1);
//如果是Long 型
BigDecimal big3 = BigDecimal.valueOf(200L); big1 = big1.subtract(big3); System.out.println("big1=" + big1);
}
}
//输出效果
BigDecimal的用法 - 賢賢 - ╉落淚的魚┽賢
2.项目中的应用
/**
* 1.降要删除的数据的paymentSummaryid设为null ;
* 2.总金额相减
*
* @param paymentId
*/
public void setPaymentSummaryIdNull(Long paymentId, Long paymentSummaryId) {
Payment payment = payMentRepository.findOne(paymentId);
PaymentSummary summary = paymentSummaryRepository.findOne(78456137l);
int count = findPaymentSummaryIdList(paymentSummaryId).size();//总数
List resultList = findPaymentSummaryIdList(paymentSummaryId);
if (count != 0) {
for (int i = 0; i < count; i++) {
if (resultList.get(i) == paymentId) { //如果list里的ID等于paymentId总数相减
PaymentSummary paymentSummary = paymentSummaryRepository.findOne(paymentSummaryId);
//paymentSummary.getSummaryFee()是BigDecimal类型,(paymentSummary是实体)
//必须得先把paymentSummary.getSummaryFee()转换成String 在转成Double
BigDecimal summaryFee = new BigDecimal
(Double.valueOf(paymentSummary.getSummaryFee().toString())); //得到总金额
BigDecimal paymentFee = new BigDecimal
(Double.valueOf(payment.getFee().toString())); //得到要修改的数据的费用
paymentSummary.setSummaryFee(summaryFee.subtract(paymentFee));//两个值相减
paymentSummary = paymentSummaryRepository.save(paymentSummary);
}
}
}
payment.getFee();
payment.setPaymentSummary(summary);
payment = payMentRepository.save(payment);
}