是否有一种简单的方法可以将洋红税改为仅向下舍入到最近的便士?

时间:2013-12-05 16:49:32

标签: php magento math magento-1.8

嗨,我只是想知道,无论最后一个数字是什么,都有一个简单的方法可以在magento中获得税收。例如:

12.56将保持12.56 12.563将向下舍入到12.56 12.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;
    }
    }
} ?>

2 个答案:

答案 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

答案 1 :(得分:0)

我发现这里发生了什么,这是一个轻微的误判我代表我正在看大桶和计算错误起初我认为它只是整体答案的20%,但它不是我的magento设置所以每一行都会计算出增值税,所以当我按每行计算出增值税时,它就是正确的。感谢您向我展示的解决方案,这将在我的项目生命周期中稍后派上用场。

相关问题