You are here

function commerce_round in Commerce Core 7

Rounds a number using the specified rounding mode.

Parameters

$round_mode: The round mode specifying which direction to round the number.

$number: The number to round.

Return value

The number rounded based on the specified mode.

See also

commerce_round_mode_options_list()

7 calls to commerce_round()
commerce_line_item_unit_price_add in modules/line_item/commerce_line_item.rules.inc
Rules action: add an amount to the unit price.
commerce_line_item_unit_price_amount in modules/line_item/commerce_line_item.rules.inc
Rules action: set the unit price to a specific amount.
commerce_line_item_unit_price_divide in modules/line_item/commerce_line_item.rules.inc
Rules action: divide the unit price by some amount.
commerce_line_item_unit_price_multiply in modules/line_item/commerce_line_item.rules.inc
Rules action: multiply the unit price by some amount.
commerce_line_item_unit_price_subtract in modules/line_item/commerce_line_item.rules.inc
Rules action: subtract an amount from the unit price.

... See full list

File

./commerce.module, line 856
Defines features and functions common to the Commerce modules.

Code

function commerce_round($round_mode, $number) {

  // Remember if this is a negative or positive number and make it positive.
  $negative = $number < 0;
  $number = abs($number);

  // Store the decimal value of the number.
  $decimal = $number - floor($number);

  // No need to round if there is no decimal value.
  if ($decimal == 0) {
    return $negative ? -$number : $number;
  }

  // Round it now according to the specified round mode.
  switch ($round_mode) {

    // PHP's round() function defaults to rounding the half up.
    case COMMERCE_ROUND_HALF_UP:
      $number = round($number);
      break;

    // PHP < 5.3.0 does not support rounding the half down, so we compare the
    // decimal value and use floor() / ceil() directly.
    case COMMERCE_ROUND_HALF_DOWN:
      if ($decimal <= 0.5) {
        $number = floor($number);
      }
      else {
        $number = ceil($number);
      }
      break;

    // PHP < 5.3.0 does not support rounding to the nearest even number, so we
    // determine it ourselves if the decimal is .5.
    case COMMERCE_ROUND_HALF_EVEN:
      if ($decimal == 0.5) {
        if (floor($number) % 2 == 0) {
          $number = floor($number);
        }
        else {
          $number = ceil($number);
        }
      }
      else {
        $number = round($number);
      }
      break;

    // PHP < 5.3.0 does not support rounding to the nearest odd number, so we
    // determine it ourselves if the decimal is .5.
    case COMMERCE_ROUND_HALF_ODD:
      if ($decimal == 0.5) {
        if (floor($number) % 2 == 0) {
          $number = ceil($number);
        }
        else {
          $number = floor($number);
        }
      }
      else {
        $number = round($number);
      }
      break;
    case COMMERCE_ROUND_NONE:
    default:
      break;
  }

  // Return the number preserving the initial negative / positive value.
  return $negative ? -$number : $number;
}