You are here

function Currency::formatAmount in Currency 8.3

Format an amount using this currency and the environment's default currency locale. pattern.

Parameters

string $amount: A numeric string.

boolean $use_currency_precision: Whether or not to use the precision (number of decimals) that the currency is configured to. If FALSE, the amount will be formatted as-is.

string $language_type: One of the \Drupal\Core\Language\LanguageInterface\TYPE_* constants.

Return value

string

Overrides CurrencyInterface::formatAmount

File

src/Entity/Currency.php, line 299

Class

Currency
Defines a currency entity class.

Namespace

Drupal\currency\Entity

Code

function formatAmount($amount, $use_currency_precision = TRUE, $language_type = LanguageInterface::TYPE_CONTENT) {
  if ($use_currency_precision && $this
    ->getSubunits()) {

    // Round the amount according the currency's configuration.
    $amount = bcmul(round(bcdiv($amount, $this
      ->getRoundingStep(), 6)), $this
      ->getRoundingStep(), 6);
    $decimal_mark_position = strpos($amount, '.');

    // The amount has no decimals yet, so add a decimal mark.
    if ($decimal_mark_position === FALSE) {
      $amount .= '.';
    }

    // Remove any existing trailing zeroes.
    $amount = rtrim($amount, '0');

    // Add the required number of trailing zeroes.
    $amount_decimals = strlen(substr($amount, $decimal_mark_position + 1));
    if ($amount_decimals < $this
      ->getDecimals()) {
      $amount .= str_repeat('0', $this
        ->getDecimals() - $amount_decimals);
    }
  }
  return $this
    ->getCurrencyAmountFormatterManager()
    ->getDefaultPlugin()
    ->formatAmount($this, $amount, $language_type);
}