/** * This method returns an amount as currency, with a symbol and currency code. * * <code> * // Create currency object. * $currencyId = 1; * $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $currencyId); * * // Create amount object. * $options = new Joomla\Registry\Registry(); * $options->set("intl", true); * $options->set("locale", "en_GB"); * $options->set("format", "2/,/."); * * $amount = 1500.25; * * $amount = new Crowdfunding\Amount($amount, $options); * $amount->setCurrency($currency); * * // Return $1,500.25 or 1,500.25USD. * echo $amount->formatCurrency(); * </code> * * @return string */ public function formatCurrency() { $intl = (bool) $this->options->get('intl', false); $fractionDigits = abs($this->options->get('fraction_digits', 2)); $format = $this->options->get('format'); $amount = $this->value; // Use number_format. if (!$intl and \JString::strlen($format) > 0) { $value = $this->formatNumber($this->value); if (!$this->currency->getSymbol()) { // Symbol $amount = $value . $this->currency->getCode(); } else { // Code if (0 === $this->currency->getPosition()) { // Symbol at beginning. $amount = $this->currency->getSymbol() . $value; } else { // Symbol at end. $amount = $value . ' ' . $this->currency->getSymbol(); } } } // Use PHP Intl library. if ($intl and extension_loaded('intl')) { // Generate currency string using PHP NumberFormatter ( Internationalization Functions ) $locale = $this->options->get('locale'); // Get current locale code. if (!$locale) { $lang = \JFactory::getLanguage(); $locale = str_replace('-', '_', $lang->getTag()); } $numberFormat = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $numberFormat->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $this->currency->getCode()); $numberFormat->setAttribute(\NumberFormatter::FRACTION_DIGITS, $fractionDigits); $amount = $numberFormat->formatCurrency($this->value, $this->currency->getCode()); } return $amount; }