嗨,我只是想知道有没有一种简单的方法可以让magento中的税款只四舍五入,无论最后一个数字是多少。例如:
12.56将保持12.5612.563将四舍五入到12.5612.569将四舍五入至12.56
无论结局如何,我都希望它四舍五入。为此,我只使用php。这里是一个代码示例,我试图使用它,它适用于大多数价格,但在一个价格上,它四舍四入,即使你是一个整数。这是我的代码,抱歉英语不好。
<?php
const TaxRate = 20;
class ThomasDudley_Tax_Calculation extends Mage_Tax_Model_Calculation
{
public function CalcTaxAmount ($Price, $TaxRate, $PriceIncludeTax = false, $Round = true)
{
$TaxRate = $TaxRate/100;
if ($PriceIncludeTax) {
$amount = $Price*(1-1/(1+$TaxRate));
} else {
$amount = $Price*$TaxRate;
}
if ($round) {
return $this->roundDown($amount);
} else {
return $this->roundDown($amount);
}
function roundDown($amount)
{
if ($amount > 0.005) return round($amount - 0.005,2);
} elseif {
($amount < -0.005) return round($amount + 0.005,2);
else return 0.0;
}
}
} ?>
如何巧妙地使用floor()
function roundDown($amount)
{
//This can be wrapped up into one line, but for the sake of showing the process.
$tmpamt = $amount * 100; //Shift the decimal point
$newamt = floor($tmpamt);
return $newamt / 100;
}
以12.569为例:
1. $tmpamt is set to 1256.9
2. $newamt is set to 1256
3. function returns 12.56
我发现了这里发生的事情,这是代表我的一个轻微的错误计算。我看了增值税,一开始计算错误。我以为这只是总答案的20%,但这不是我的magento设置,所以增值税是按每行计算的。所以当我按每行算出增值税时,它是正确的。感谢你向我展示的解决方案,这将在我的项目生命周期中派上用场。