Int值“21”变成因子1.21

时间:2018-02-08 20:36:12

标签: php

我想将一个int值(21,VAT)变成一个浮点数(因子)1.21。这样我就可以将产品价格变成含税的价格。

某些函数给了我int 21,我很乐意使用它。我到目前为止使用过;

真的很难看

$taxRate = 21; // this come from a function in PrestaShop in case you wonder
$factor = (float)"1.$taxRate"; // 1.21

感觉更精明

$taxRate = 21;
$factor = 1+($taxRate/100); // 1.21

我真的认为我缺少其他有趣语法的功能。我知道这似乎微不足道,但我觉得这两个选项都是如此丑陋和冗长,甚至可能会在以后再次发布。

1 个答案:

答案 0 :(得分:-1)

想要这样的东西?哈哈)不要让它变得更难!

class BeatifulTaxFactorCalculator
{
    private $taxRate;

    /**
     * @return mixed
     */
    public function getTaxRate()
    {
        return $this->taxRate;
    }

    /**
     * @param mixed $taxRate
     */
    public function setTaxRate($taxRate)
    {
        $this->taxRate = $taxRate;
    }
    private function getBeautifulTaxRate()
    {
        return $this->getTaxRate()/100;
    }

    public function getBeautifulPriceFactor(){
        return 1 + $this->getBeautifulTaxRate();
    }

    final static function calculatePriceFactorReallyUgly($taxRate){
        return (float)"1.$taxRate";
    }

    final static function calculatePriceFactorMoreSavvy($taxRate){
        return 1+($taxRate/100);
    }
}

$factorUgly = BeatifulTaxFactorCalculator::calculatePriceFactorReallyUgly(21);
$factorUgly2 = BeatifulTaxFactorCalculator::calculatePriceFactorMoreSavvy(21);

$factorCalculator = new BeatifulTaxFactorCalculator();
$factorCalculator->setTaxRate(21);
$factorBeautiful = $factorCalculator->getBeautifulPriceFactor();