|
|
@@ -46,12 +46,43 @@ public class SM3Util {
|
|
|
//
|
|
|
// }
|
|
|
|
|
|
+ private static final BigDecimal MIN_THRESHOLD = new BigDecimal("0.01"); // 最小阈值
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算千分之6并处理临界值
|
|
|
+ *
|
|
|
+ * @param input 输入金额(单位:元)
|
|
|
+ * @return 计算结果(保留两位小数,不足0.01按0.01计算)
|
|
|
+ * @throws IllegalArgumentException 输入为null或负数时抛出
|
|
|
+ */
|
|
|
+ public static BigDecimal calculate(BigDecimal input, BigDecimal multiplier) {
|
|
|
+ if (input == null) {
|
|
|
+ throw new IllegalArgumentException("输入金额不能为null");
|
|
|
+ }
|
|
|
+ if (input.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ throw new IllegalArgumentException("输入金额不能为负数");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 精确乘法运算(保留4位中间精度)
|
|
|
+ BigDecimal product = input.multiply(multiplier)
|
|
|
+ .setScale(4, RoundingMode.HALF_UP);
|
|
|
+
|
|
|
+ // 临界值判断与补偿
|
|
|
+ return product.compareTo(MIN_THRESHOLD) < 0
|
|
|
+ ? MIN_THRESHOLD
|
|
|
+ : product.setScale(2, RoundingMode.DOWN);
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
+ BigDecimal price = new BigDecimal("0.3");
|
|
|
+
|
|
|
+ BigDecimal FEE = calculate(price, new BigDecimal("0.006"));
|
|
|
|
|
|
+ price = price.subtract(FEE);
|
|
|
|
|
|
- BigDecimal total1 = new BigDecimal("0.09");
|
|
|
- BigDecimal[] ratios1 = {new BigDecimal("69.4"), new BigDecimal("20"), new BigDecimal("10.6")}; // 总和100 → 直接分配
|
|
|
+ BigDecimal total1 = price;
|
|
|
+ BigDecimal[] ratios1 = {new BigDecimal("89.4"), new BigDecimal("0"), new BigDecimal("10.6")}; // 总和100 → 直接分配
|
|
|
System.out.println("示例1结果:" + Arrays.toString(allocate(total1, ratios1)));
|
|
|
System.out.println(amount_fee(new BigDecimal(0.01)).multiply(new BigDecimal(0.06)).setScale(0, RoundingMode.UP));
|
|
|
}
|