public function round($decimalDigits = null, $mode = self::ROUND_HALF_AWAY_FROM_ZERO)
    {
        if (null === $decimalDigits) {
            $decimalDigits = $this->currency->getDecimalDigits();
        }

        $factor = bcpow('10', $decimalDigits, self::BCSCALE);
        $amount = bcmul($this->amount, $factor, self::BCSCALE);

        switch ($mode) {
            case self::ROUND_UP:
                $result = self::roundUp($amount);
                break;
            case self::ROUND_DOWN:
                $result = self::roundDown($amount);
                break;
            case self::ROUND_TOWARDS_ZERO:
                $result = self::roundTowardsZero($amount);
                break;
            case self::ROUND_AWAY_FROM_ZERO:
                $result = self::roundAwayFromZero($amount);
                break;
            case self::ROUND_HALF_UP:
                $result = self::roundHalfUp($amount);
                break;
            case self::ROUND_HALF_DOWN:
                $result = self::roundHalfDown($amount);
                break;
            case self::ROUND_HALF_TOWARDS_ZERO:
                $result = self::roundHalfTowardsZero($amount);
                break;
            case self::ROUND_HALF_AWAY_FROM_ZERO:
                $result = self::roundHalfAwayFromZero($amount);
                break;
            case self::ROUND_HALF_TO_EVEN:
                $result = self::roundHalfToEven($amount);
                break;
            case self::ROUND_HALF_TO_ODD:
                $result = self::roundHalfToOdd($amount);
                break;
            default:
                throw new \InvalidArgumentException('Unknown rounding mode \''.$mode.'\'');
        }

        $result = bcdiv($result, $factor, self::BCSCALE);

        return self::valueOf($result, $this->currency);
    }
public function round($decimalDigits = null, $mode = self::ROUND_HALF_AWAY_FROM_ZERO)
    {
        if (null === $decimalDigits) {
            $decimalDigits = $this->currency->getDecimalDigits();
        }

        $factor = bcpow('10', $decimalDigits, self::BCSCALE);
        $amount = bcmul($this->amount, $factor, self::BCSCALE);

        switch ($mode) {
            case self::ROUND_UP:
                $result = self::roundUp($amount);
                break;
            case self::ROUND_DOWN:
                $result = self::roundDown($amount);
                break;
            case self::ROUND_TOWARDS_ZERO:
                $result = self::roundTowardsZero($amount);
                break;
            case self::ROUND_AWAY_FROM_ZERO:
                $result = self::roundAwayFromZero($amount);
                break;
            case self::ROUND_HALF_UP:
                $result = self::roundHalfUp($amount);
                break;
            case self::ROUND_HALF_DOWN:
                $result = self::roundHalfDown($amount);
                break;
            case self::ROUND_HALF_TOWARDS_ZERO:
                $result = self::roundHalfTowardsZero($amount);
                break;
            case self::ROUND_HALF_AWAY_FROM_ZERO:
                $result = self::roundHalfAwayFromZero($amount);
                break;
            case self::ROUND_HALF_TO_EVEN:
                $result = self::roundHalfToEven($amount);
                break;
            case self::ROUND_HALF_TO_ODD:
                $result = self::roundHalfToOdd($amount);
                break;
            default:
                throw new \InvalidArgumentException('Unknown rounding mode \''.$mode.'\'');
        }

        $result = bcdiv($result, $factor, self::BCSCALE);

        return self::valueOf($result, $this->currency);
    }
public function round ( $ decimalDigits = null , $ mode = self :: ROUND_HALF_AWAY_FROM_ZERO ) { if ( null === $ decimalDigits ) { $ decimalDigits = $ this -> currency -> getDecimalDigits ( ) ; } $ factor = bcpow ( '10' , $ decimalDigits , self :: BCSCALE ) ; $ amount = bcmul ( $ this -> amount , $ factor , self :: BCSCALE ) ; switch ( $ mode ) { case self :: ROUND_UP : $ result = self :: roundUp ( $ amount ) ; break ; case self :: ROUND_DOWN : $ result = self :: roundDown ( $ amount ) ; break ; case self :: ROUND_TOWARDS_ZERO : $ result = self :: roundTowardsZero ( $ amount ) ; break ; case self :: ROUND_AWAY_FROM_ZERO : $ result = self :: roundAwayFromZero ( $ amount ) ; break ; case self :: ROUND_HALF_UP : $ result = self :: roundHalfUp ( $ amount ) ; break ; case self :: ROUND_HALF_DOWN : $ result = self :: roundHalfDown ( $ amount ) ; break ; case self :: ROUND_HALF_TOWARDS_ZERO : $ result = self :: roundHalfTowardsZero ( $ amount ) ; break ; case self :: ROUND_HALF_AWAY_FROM_ZERO : $ result = self :: roundHalfAwayFromZero ( $ amount ) ; break ; case self :: ROUND_HALF_TO_EVEN : $ result = self :: roundHalfToEven ( $ amount ) ; break ; case self :: ROUND_HALF_TO_ODD : $ result = self :: roundHalfToOdd ( $ amount ) ; break ; default : throw new \ InvalidArgumentException ( 'Unknown rounding mode \'' . $ mode . '\'' ) ; } $ result = bcdiv ( $ result , $ factor , self :: BCSCALE ) ; return self :: valueOf ( $ result , $ this -> currency ) ; }
Round to a given number of decimal digits, using a given mode.

@param int    $decimalDigits the digits to truncate to, null for currency default
@param string $mode          the rounding mode to use, see class constants

@return Money
Round to a given number of decimal digits using a given mode .