java_涉及金额计算

关于常规计算金额精度丢失的问题,需要使用BigDecimal类处理,

 synchronized (CheckoutSync.class) {
                    extendBean = baseDaoService.selectEntityInfoByInfo("com.sunwayworld.lims4.rasclients.sql.selectContractByClearanceid", extendBean);
                    if (null != extendBean && !"".equals(extendBean)) {
                        String contractid = extendBean.get("contractid").toString();
                        BigDecimal cbalance = new BigDecimal(extendBean.get("cbalance").toString());
                        BigDecimal sptestcheckfee = new BigDecimal(extendBean.get("sptestcheckfee").toString());
                        BigDecimal discount = new BigDecimal(extendBean.get("discount").toString());
                        if (sptestcheckfee.compareTo(BigDecimal.ZERO) == -1 || discount.compareTo(BigDecimal.ZERO) == -1) {
                            throw new RunException("食品委托[检验费]或者[折扣]不能为负数!");
                        }
                        sptestcheckfee = sptestcheckfee.multiply(discount);
                        String cbalanceNow = cbalance.subtract(sptestcheckfee).toString();
                        contract.setContractid(contractid);
                        contract.setCbalance(cbalanceNow);
                        baseDaoService.updateEntityInfo(contract);
                    }
                }

//compareTo函数:参考致谢:https://www.cnblogs.com/Absorbed/p/10487379.html

//关于加锁,参考致谢:https://blog.csdn.net/xuqiaobo/article/details/51513871

发布了129 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zs_life/article/details/103952128
今日推荐