fix: 修复优惠券金额分摊计算精度问题 (Issue #356)#967
Open
Senrian wants to merge 2 commits intomacrozheng:masterfrom
Open
Conversation
问题:使用 (商品原价/总价)*满减金额 的除法优先顺序,当满减金额为0.98时, 由于divide精度为3位HALF_EVEN,计算结果可能产生如0.979这样的多位小数, 导致最终优惠金额少了约0.02元。 修复:改为 乘法优先再除法 originalPrice.multiply(reducePrice).divide(totalAmount, 2, RoundingMode.HALF_EVEN), 保证最终结果精确到分(2位小数),符合人民币货币精度。 同时确保使用 RoundingMode.HALF_EVEN 保持与原有代码一致。 Fixes macrozheng#492
原算法对每个商品独立计算分摊金额并四舍五入到3位小数, 导致各商品分摊金额之和可能小于优惠券面额。 修复方案: - 对前 n-1 个商品按比例计算分摊金额 - 最后一个商品获取剩余金额,确保分摊总额精确等于优惠券面额 - 最终金额四舍五入到2位小数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug 描述
Issue: #356
OmsPortalOrderServiceImpl.calcPerCouponAmount()方法在计算优惠券分摊时,对每个商品独立计算并四舍五入到3位小数(HALF_EVEN),导致各商品分摊金额之和可能小于优惠券面额。复现案例:
修复方案
对前
n-1个商品按比例计算分摊金额,最后一个商品获取剩余金额,确保分摊总额精确等于优惠券面额。最终金额四舍五入到2位小数。修改文件
mall-portal/src/main/java/com/macro/mall/portal/service/impl/OmsPortalOrderServiceImpl.javaFixes #356