0

Magento 製品を最も近い $x.90、$x.80、$x.85 などに丸める次のコードがありますが、入力は手動です。私が必要としているのは、ボタンを押すだけで価格を 10 セント単位で表示する新しい機能です。したがって、価格が $11.87 の場合は $11.90 にする必要があり、価格が $11.63 の場合は $11.60 にする必要があります。

どうすればいいですか?

protected function _round($price, $roundTo = 0.00, $type = 'normal')
{
    $roundTo = ltrim(number_format($roundTo, 2, '.', ''), '0');

    if ($type === 'normal') {
        return round($price);
    }

    $safety = 99999999;

    if ($type === 'up' || ($type === 'down' && $roundTo < $price)) {
        while(substr($price, -(strlen($roundTo))) != $roundTo) {

            if ($type === 'up') {
                $price += .01;
            }
            else {
                $price -= .01;
            }

            $price = number_format($price, 2, '.', '');

            if (--$safety < 1) {
                throw new Exception('Error');
            }
        }

        return $price;
    }

    return false;
}   
4

1 に答える 1

1

この小さな機能は機能します。

function roundPrice($price){
    $rounded = round($price, 1);
    return number_format($rounded, 2);
}

$newprice = roundPrice($InputYourValueHere);

// Output the value for the purposes of the example
echo $newprice;
于 2013-02-15T09:45:22.377 に答える