You are here

public function Basic::formatAmount in Currency 8.3

Formats an amount.

Parameters

\Commercie\Currency\CurrencyInterface $currency: The currency the amount is in.

string $amount: A numeric string.

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

Return value

string|\Drupal\Core\StringTranslation\TranslatableMarkup

Overrides AmountFormatterInterface::formatAmount

File

src/Plugin/Currency/AmountFormatter/Basic.php, line 61

Class

Basic
Formats amounts using string translation and number_format().

Namespace

Drupal\currency\Plugin\Currency\AmountFormatter

Code

public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT) {

  // Compute the number of decimals, so we can format all of them and no less
  // or more.
  $decimals = strlen($amount) - strpos($amount, '.') - 1;
  $currency_locale = $this->localeDelegator
    ->resolveCurrencyLocale();
  $formatted_amount = number_format($amount, $decimals, $currency_locale
    ->getDecimalSeparator(), $currency_locale
    ->getGroupingSeparator());
  $arguments = array(
    '@currency_code' => $currency
      ->getCurrencyCode(),
    '@currency_sign' => $currency
      ->getSign(),
    '@amount' => $formatted_amount,
  );
  return $this
    ->t('@currency_code @amount', $arguments);
}