Prestashop: Deprecated Tools::ps_round in favor of PrestaShop Decimal

Created on 24 Apr 2020  路  3Comments  路  Source: PrestaShop/PrestaShop

Describe the bug

The Tools::ps:round function rounds to 0 precision if no parameter is sent.

Expected behavior

The function should round to precision chosen in config, as it is written in the comment for the function.

Additional information

  • PrestaShop version: 1.7.6.5
  • PHP version: N/A
Bug CO Developer Feature To Do

Most helpful comment

I think this function should be deprecated in favor of Decimal.

All 3 comments

Thanks for opening this issue! We will help you to keep its state consistent

Hi @PrestaworksNiklas and thanks for your report.

I confirm the function is malformed and need to be a little refactored.
https://github.com/PrestaShop/PrestaShop/blob/1.7.7.x/classes/Tools.php#L1897

The Tools::$round_mode property shouldn't be public nor set in cache.

    /**
     * Returns the rounded value of $value to specified precision, according to your configuration;.
     *
     * @param float $value
     * @param int $precision
     * @param int $round_mode
     *
     * @return float
     */
    public static function ps_round($value, $precision = 0, $round_mode = null)
    {
        if ($round_mode === null) {
            $round_mode = (int) Configuration::get('PS_PRICE_ROUND_MODE');
        }

        switch ($round_mode) {
            case PS_ROUND_UP:
                return Tools::ceilf($value, $precision);
            case PS_ROUND_DOWN:
                return Tools::floorf($value, $precision);
            case PS_ROUND_HALF_DOWN:
            case PS_ROUND_HALF_EVEN:
            case PS_ROUND_HALF_ODD:
                return Tools::math_round($value, $precision, $round_mode);
            case PS_ROUND_HALF_UP:
            default:
                return Tools::math_round($value, $precision, PS_ROUND_HALF_UP);
        }
    }

@PrestaShop/prestashop-core-developers What do you think?

I think this function should be deprecated in favor of Decimal.

Was this page helpful?
0 / 5 - 0 ratings