You are here

function commerce_currency_decimal_to_amount in Commerce Core 7

Converts a price amount to an integer value for storage in the database.

Parameters

$decimal: The decimal amount to convert to a price amount.

$currency_code: The currency code of the price whose decimals value will be used to multiply by the proper factor when converting the decimal amount.

$round: Whether or not the return value should be rounded and cast to an integer; defaults to TRUE as necessary for standard price amount column storage.

Return value

The appropriate price amount based on the currency's decimals value.

6 calls to commerce_currency_decimal_to_amount()
commerce_order_update_7100 in modules/order/commerce_order.install
Loads and resaves all the products on the site, updating the default price field to have proper component price amount values.
commerce_order_update_7101 in modules/order/commerce_order.install
Loads and resaves all the line items on the site, updating the unit price field to have proper component price amount values.
commerce_payment_order_transaction_add_form_payment_terminal_validate in modules/payment/includes/commerce_payment.forms.inc
Validation callback for the payment terminal to check the amount data type and convert it to a proper integer amount on input.
commerce_price_field_widget_validate in modules/price/commerce_price.module
Validate callback: ensures the amount value is numeric and converts it from a decimal value to an integer price amount.
commerce_price_handler_filter_commerce_price_amount::query in modules/price/includes/views/handlers/commerce_price_handler_filter_commerce_price_amount.inc
Add this filter to the query.

... See full list

File

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

Code

function commerce_currency_decimal_to_amount($decimal, $currency_code, $round = TRUE) {
  static $factors;

  // If the divisor for this currency hasn't been calculated yet...
  if (empty($factors[$currency_code])) {

    // Load the currency and calculate its factor as a power of 10.
    $currency = commerce_currency_load($currency_code);
    $factors[$currency_code] = pow(10, $currency['decimals']);
  }

  // Ensure the amount has the proper number of decimal places for the currency.
  if ($round) {
    $decimal = commerce_currency_round($decimal, commerce_currency_load($currency_code));
    return (int) round($decimal * $factors[$currency_code]);
  }
  else {
    return $decimal * $factors[$currency_code];
  }
}