You are here

function _money_amount_to_integer in Money field 5

Convert the amount (a string entered by the user) to an integer.

Parameters

$amount: The amount to convert.

Return value

The corresponding integer.

2 calls to _money_amount_to_integer()
money_views_handler_filter_amount in ./money.module
Views filter handler: amount filter.
money_widget in ./money.module
Implementation of hook_widget().

File

./money.module, line 513
This module defines the "money" CCK field. It uses the Currency API, which is included in the Currency module, to get a list of valid currencies.

Code

function _money_amount_to_integer($amount, $decimal_separator, $digit_group_separator) {
  $decimal_separator = _money_get_decimal_separator($decimal_separator);
  $digit_group_separator = _money_get_digit_group_separator($digit_group_separator);

  // Convert the entered amount to be compatible with PHP's number
  // notation: a dot as a decimal separator, nothing as a digit
  // group separator.
  $converted_amount = str_replace(array(
    $decimal_separator,
    $digit_group_separator,
  ), array(
    '.',
    '',
  ), $amount);

  // Now convert the amount to make it storable as an integer.
  // We are always working with a maximum of 2 decimals, this means
  // that one unit in the database corresponds to 1/100th of a unit
  // in reality (i.e. in forms and on display).
  $converted_amount *= 100;

  // PHP's conversion from floats to ints is choppy at best, so make sure it
  // gets converted properly.
  $converted_amount = $converted_amount < 0 ? (int) ($converted_amount - 0.01) : (int) ($converted_amount + 0.01);
  return $converted_amount;
}