税率计算
This commit is contained in:
parent
b1cafdc811
commit
09a91c35f8
|
@ -0,0 +1,40 @@
|
||||||
|
package com.hzya.frame.plugin.cinvoice.util;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 税额计算器
|
||||||
|
* @Author xiangerlin
|
||||||
|
* @Date 2025/5/9 11:26
|
||||||
|
**/
|
||||||
|
public class TaxCalculator {
|
||||||
|
/**
|
||||||
|
* 计算税额的方法
|
||||||
|
*
|
||||||
|
* @param amountWithTax 含税金额(BigDecimal)
|
||||||
|
* @param taxRate 税率(BigDecimal,例如 new BigDecimal("0.13") 表示 13%)
|
||||||
|
* @return 税额(BigDecimal)
|
||||||
|
*/
|
||||||
|
public static BigDecimal calculateTax(BigDecimal amountWithTax, BigDecimal taxRate) {
|
||||||
|
if (amountWithTax == null || taxRate == null || taxRate.compareTo(BigDecimal.ZERO) < 0) {
|
||||||
|
throw new IllegalArgumentException("参数错误");
|
||||||
|
}
|
||||||
|
// 不含税金额 = 含税金额 / (1 + 税率)
|
||||||
|
BigDecimal one = BigDecimal.ONE;
|
||||||
|
BigDecimal taxInclusiveRate = one.add(taxRate);
|
||||||
|
BigDecimal amountWithoutTax = amountWithTax.divide(taxInclusiveRate, 6, BigDecimal.ROUND_HALF_UP);
|
||||||
|
|
||||||
|
// 税额 = 含税金额 - 不含税金额
|
||||||
|
BigDecimal tax = amountWithTax.subtract(amountWithoutTax).setScale(2, BigDecimal.ROUND_HALF_UP);
|
||||||
|
return tax;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 示例:含税金额为 113,税率为 13%
|
||||||
|
BigDecimal amountWithTax = new BigDecimal("568");
|
||||||
|
BigDecimal taxRate = new BigDecimal("0.09");
|
||||||
|
|
||||||
|
BigDecimal tax = calculateTax(amountWithTax, taxRate);
|
||||||
|
System.out.println("税额为: " + tax); // 输出结果应为 13.00
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue