PHP:计算适用于产品的百分比税
PHP: Calculating percentage tax applied to a product
由于四舍五入,我在计算适用于产品的百分比税时遇到了重大问题。
例如:
如果您的产品价格为 1.00 英镑,含 20% 税,则细分为:
0.83 ex tax
0.17 tax
1.00 total
但是,如果计算出增加的百分比:
round( (( ( 1 - 0.83 ) / 0.83 ) * 100), 2);
答案是20.48
,因为不含税的实际价格是0.8333333333
因此如果你计算:
round( (( ( 1 - 0.8333333333 ) /0.8333333333 ) * 100), 2);
你得到 20
的正确答案。
在这种情况下,将 20.48
向下舍入到 20
显然可行,但这不是解决方案,因为某些税率是小数点后两位,因此无法做出假设你可以四舍五入税率。
我是不是遗漏了什么,或者在不知道原始税率的情况下这不可能吗?
只要你处理的是低精度的数字,你就会得到低精度的答案。如果一定要显示低精度的数字,在视图中显示给用户时可以四舍五入,但在数据库中保存为高精度数字。
0.17
不是 0.83
的 20%
,所以你的基本假设是不准确的(四舍五入 :P )。
不要四舍五入,计算时不要四舍五入,必要时四舍五入。这避免了不得不降低计算精度。
一个简单的测试将证明
$price=0.8333333333;
$taxRate=21.25;
$tax=$price*$taxRate/100;
$total=$price+$tax;
$calculatedTaxRate=(($total-$price)/$price)*100; // 21.25
因为我们没有四舍五入,所以我们可以对税率进行逆向工程,始终精确到点。
试试 20%
$price=0.8333333333;
$taxRate=20;
$tax=$price*$taxRate/100;
$total=$price+$tax;
$calculatedTaxRate=(($total-$price)/$price)*100; // 20
会不会像这样:
17% 增值税
85.47 免税
85.47x0.17 = 14.53
总计:100
所以 100/1.17 = 85.47
假设您的总价为 1 英镑,税率为 20%:
$vatDecimal = (float) 1 + (20 / 100);
$priceExclVAT = round((1.00 / $vatDecimal), 2);
$priceDisplay = number_format($priceExclVAT, 2, ',', '.');
echo $priceDisplay;
我的little-and-dirty版本:
$taxrate=22; // 22%
$price=100;
$priceMinusTax=($price/(($taxrate/100)+1));
用户输入总金额和税金。有时它来自数据库,我们也可以使用这个代码。
$actualPrice = "";
$total = 1000;//User Entry Total Amount
$gst = 18;//User Entry GST Tax %
$calculateTax = 100+$gst;
$calculateAmount = $total*100;
$actualPrice = $calculateAmount/$calculateTax;
echo $actualPrice = round($actualPrice,2);
echo $gstAmount = round($total-$actualPrice,2);
由于四舍五入,我在计算适用于产品的百分比税时遇到了重大问题。
例如:
如果您的产品价格为 1.00 英镑,含 20% 税,则细分为:
0.83 ex tax
0.17 tax
1.00 total
但是,如果计算出增加的百分比:
round( (( ( 1 - 0.83 ) / 0.83 ) * 100), 2);
答案是20.48
,因为不含税的实际价格是0.8333333333
因此如果你计算:
round( (( ( 1 - 0.8333333333 ) /0.8333333333 ) * 100), 2);
你得到 20
的正确答案。
在这种情况下,将 20.48
向下舍入到 20
显然可行,但这不是解决方案,因为某些税率是小数点后两位,因此无法做出假设你可以四舍五入税率。
我是不是遗漏了什么,或者在不知道原始税率的情况下这不可能吗?
只要你处理的是低精度的数字,你就会得到低精度的答案。如果一定要显示低精度的数字,在视图中显示给用户时可以四舍五入,但在数据库中保存为高精度数字。
0.17
不是 0.83
的 20%
,所以你的基本假设是不准确的(四舍五入 :P )。
不要四舍五入,计算时不要四舍五入,必要时四舍五入。这避免了不得不降低计算精度。
一个简单的测试将证明
$price=0.8333333333;
$taxRate=21.25;
$tax=$price*$taxRate/100;
$total=$price+$tax;
$calculatedTaxRate=(($total-$price)/$price)*100; // 21.25
因为我们没有四舍五入,所以我们可以对税率进行逆向工程,始终精确到点。
试试 20%
$price=0.8333333333;
$taxRate=20;
$tax=$price*$taxRate/100;
$total=$price+$tax;
$calculatedTaxRate=(($total-$price)/$price)*100; // 20
会不会像这样:
17% 增值税
85.47 免税
85.47x0.17 = 14.53
总计:100
所以 100/1.17 = 85.47
假设您的总价为 1 英镑,税率为 20%:
$vatDecimal = (float) 1 + (20 / 100);
$priceExclVAT = round((1.00 / $vatDecimal), 2);
$priceDisplay = number_format($priceExclVAT, 2, ',', '.');
echo $priceDisplay;
我的little-and-dirty版本:
$taxrate=22; // 22%
$price=100;
$priceMinusTax=($price/(($taxrate/100)+1));
用户输入总金额和税金。有时它来自数据库,我们也可以使用这个代码。
$actualPrice = "";
$total = 1000;//User Entry Total Amount
$gst = 18;//User Entry GST Tax %
$calculateTax = 100+$gst;
$calculateAmount = $total*100;
$actualPrice = $calculateAmount/$calculateTax;
echo $actualPrice = round($actualPrice,2);
echo $gstAmount = round($total-$actualPrice,2);