/** * Get price with currency only if data is not null * (if data is null and formatted by formatCurrency(), it will return 0) * * @param \NumberFormatter $numberFormatter * @param array $price * * @return string */ protected function getPrice(\NumberFormatter $numberFormatter, array $price) { if (!isset($price['data'])) { return ''; } return $numberFormatter->formatCurrency($price['data'], $price['currency']); }
/** * \copydoc ::Erebot::Styling::VariableInterface::render() * * \note * If no currency was passed to this class' constructor, * the currency associated with the translator's locale * is used. */ public function render(\Erebot\IntlInterface $translator) { $locale = $translator->getLocale(\Erebot\IntlInterface::LC_MONETARY); $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $currency = $this->currency !== null ? $this->currency : $formatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL); return (string) $formatter->formatCurrency($this->value, $currency); }
/** * {@inheritdoc} */ public function format($amount, $currency, $locale = 'en') { $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $result = $formatter->formatCurrency($amount / 100, $currency); Assert::notSame(false, $result, sprintf('The amount "%s" of type %s cannot be formatted to currency "%s".', $amount, gettype($amount), $currency)); return $result; }
public function currency($value, $currency = 'USD') { // use of NumberFormatter from extension package intl // $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); return $formatter->formatCurrency($value, $currency); }
/** * Returns the pattern for this locale. * * The pattern contains the placeholder "{{ widget }}" where the HTML tag should * be inserted */ protected static function getPattern($currency) { if (!$currency) { return '{{ widget }}'; } $locale = \Locale::getDefault(); if (!isset(self::$patterns[$locale])) { self::$patterns[$locale] = array(); } if (!isset(self::$patterns[$locale][$currency])) { $format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $pattern = $format->formatCurrency('123', $currency); // the spacings between currency symbol and number are ignored, because // a single space leads to better readability in combination with input // fields // the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8) preg_match('/^([^\\s\\xc2\\xa0]*)[\\s\\xc2\\xa0]*123(?:[,.]0+)?[\\s\\xc2\\xa0]*([^\\s\\xc2\\xa0]*)$/u', $pattern, $matches); if (!empty($matches[1])) { self::$patterns[$locale][$currency] = $matches[1] . ' {{ widget }}'; } elseif (!empty($matches[2])) { self::$patterns[$locale][$currency] = '{{ widget }} ' . $matches[2]; } else { self::$patterns[$locale][$currency] = '{{ widget }}'; } } return self::$patterns[$locale][$currency]; }
public function notify(RequestVerifiedEvent $event) { $payment = $event->getPayment(); $status = $event->getStatus()->getValue(); switch ($status) { case GetHumanStatus::STATUS_AUTHORIZED: case GetHumanStatus::STATUS_CAPTURED: case GetHumanStatus::STATUS_REFUNDED: $this->repository->clearCart(); $type = 'success'; break; case GetHumanStatus::STATUS_CANCELED: case GetHumanStatus::STATUS_EXPIRED: case GetHumanStatus::STATUS_FAILED: $type = 'danger'; break; case GetHumanStatus::STATUS_PENDING: case GetHumanStatus::STATUS_SUSPENDED: $this->repository->clearCart(); $type = 'warning'; break; case GetHumanStatus::STATUS_NEW: case GetHumanStatus::STATUS_UNKNOWN: $this->repository->clearCart(); $type = 'info'; break; default: throw new \RuntimeException('Unknown status ' . $status); } $formatter = new \NumberFormatter($this->translator->getLocale(), \NumberFormatter::CURRENCY); $this->session->getFlashBag()->add($type, $this->translator->trans('flash.payment.' . $type, ['%status%' => $this->translator->trans('meta.status.' . $status), '%amount%' => $formatter->formatCurrency($payment->getTotalAmount() / 100, $payment->getCurrencyCode())])); }
/** * Returns the pattern for this locale * * The pattern contains the placeholder "{{ widget }}" where the HTML tag should * be inserted */ public function getPattern() { if (!$this->getOption('currency')) { return '{{ widget }}'; } if (!isset(self::$patterns[$this->locale])) { self::$patterns[$this->locale] = array(); } if (!isset(self::$patterns[$this->locale][$this->getOption('currency')])) { $format = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY); $pattern = $format->formatCurrency('123', $this->getOption('currency')); // the spacings between currency symbol and number are ignored, because // a single space leads to better readability in combination with input // fields // the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8) preg_match('/^([^\\s\\xc2\\xa0]*)[\\s\\xc2\\xa0]*123[,.]00[\\s\\xc2\\xa0]*([^\\s\\xc2\\xa0]*)$/', $pattern, $matches); if (!empty($matches[1])) { self::$patterns[$this->locale] = $matches[1] . ' {{ widget }}'; } else { if (!empty($matches[2])) { self::$patterns[$this->locale] = '{{ widget }} ' . $matches[2]; } else { self::$patterns[$this->locale] = '{{ widget }}'; } } } return self::$patterns[$this->locale]; }
/** * returns a string with currency formatted accordingly to locale settings * @param string $value * @param string $decimals * @param string $currencyCode * @return string */ public static function currency($value, $decimals = 2, $currencyCode = null) { $app = Application::instance(); $currencyCode = $currencyCode ?: $app->getConfig('app.settings.currency'); $nf = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::CURRENCY); $nf->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimals); return $nf->formatCurrency($value, $currencyCode); }
/** * localized format for money with the NumberFormatter class in Currency mode * with currency symbol * like €1.234,56 * * @param Money $money * @param null $locale * @return bool|string */ public function formatI18n(Money $money, $locale = null) { if ($locale === null) { $locale = $this->locale; } $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); return $formatter->formatCurrency($money->getConvertedAmount(), $money->getCurrency()->getCurrencyCode()); }
/** * @todo This is the easiet way I could see to get a currency from a String such as 'GBP' * However it's not a very nice solution. This should probably be updated in the future. */ public function currencySymbolFunction($currency = null, $locale = null) { $locale = $locale == null ? \Locale::getDefault() : $locale; $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $currency = $currency ?: $this->_defaultCurrency; $symbol = substr($formatter->formatCurrency(0, $currency), 0, -4); return $symbol; }
/** * {@inheritDoc} */ public function format($value, $valueCurrency = null, $decimal = true, $symbol = true) { $formatter = new \NumberFormatter($this->locale, $symbol ? \NumberFormatter::CURRENCY : \NumberFormatter::PATTERN_DECIMAL); $value = $formatter->formatCurrency($value, $valueCurrency); if (!$decimal) { $value = preg_replace('/[.,]00((?=\\D)|$)/', '', $value); } if (count($this->cleanCharacters) > 0) { $value = str_replace($this->cleanCharacters, '', $value); } return $value; }
/** * {@inheritdoc} */ public function format(Money $money) { $valueBase = (string) $money->getAmount(); $negative = false; if (substr($valueBase, 0, 1) === '-') { $negative = true; $valueBase = substr($valueBase, 1); } $fractionDigits = $this->formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS); $valueLength = strlen($valueBase); if ($valueLength > $fractionDigits) { $subunits = substr($valueBase, 0, $valueLength - $fractionDigits) . '.'; $subunits .= substr($valueBase, $valueLength - $fractionDigits); } else { $subunits = '0.' . str_pad('', $fractionDigits - $valueLength, '0') . $valueBase; } if ($negative === true) { $subunits = '-' . $subunits; } return $this->formatter->formatCurrency($subunits, $money->getCurrency()->getCode()); }
protected function set_up_display() { if (isset($this->category_id)) { $category = Category::find_by_id((int) $this->category_id); $this->category = $category->category; // $this->unit_price = $category->unit_price; $this->price = $this->unit_price * $this->quantity; $this->price_company = $this->company_unit_price * $this->quantity; if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { $formatter = new NumberFormatter('en_CH', NumberFormatter::CURRENCY); $this->price = $formatter->formatCurrency($this->price, 'CHF'); $this->price_company = $formatter->formatCurrency($this->price_company, 'CHF'); // $this->unit_price=$formatter->formatCurrency($this->unit_price, 'CHF'); // $this->company_unit_price=$formatter->formatCurrency($this->company_unit_price, 'CHF'); } } if (isset($this->project_id)) { $project = Project::find_by_id((int) $this->project_id); $this->project_code = $project->project_code; } }
/** * {@inheritdoc} */ public function format($amount, $currency = null, $locale = null) { if (null === $currency) { $currency = $this->requestHelper->getCurrentCurrency(); } $locale = $this->getLocale($locale); $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); if (false === ($result = $formatter->formatCurrency($amount, $currency))) { throw new CurrencyFormatterException($amount, $currency, $locale); } return $result; }
/** * {@inheritdoc} */ public function format(Money $money) { $valueBase = (string) $money->getAmount(); $negative = false; if ($valueBase[0] === '-') { $negative = true; $valueBase = substr($valueBase, 1); } $subunit = $this->currencies->subunitFor($money->getCurrency()); $valueLength = strlen($valueBase); if ($valueLength > $subunit) { $formatted = substr($valueBase, 0, $valueLength - $subunit) . '.'; $formatted .= substr($valueBase, $valueLength - $subunit); } else { $formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase; } if ($negative === true) { $formatted = '-' . $formatted; } return $this->formatter->formatCurrency($formatted, $money->getCurrency()->getCode()); }
/** * money * * @param float|\browner12\money\Money $money * @param string $currency * @return string */ function money($money, $currency = 'usd') { //money object if ($money instanceof \browner12\money\Money) { $currency = $money->getCurrency()->currency(); $money = $money->value(); } //formatter $cf = new NumberFormatter('eng', NumberFormatter::CURRENCY); //return return $cf->formatCurrency($money, $currency); }
/** * Format the given value. * * @param mixed $value * @param string $valueCurrency $value currency code * @param boolean $decimal show decimal part * @param boolean $symbol show currency symbol * @return string */ public function format($value, $valueCurrency = null, $decimal = true, $symbol = true) { if (null === $valueCurrency) { $valueCurrency = $this->getConverter()->getDefaultCurrency(); } $formatter = new \NumberFormatter($this->translator->getLocale(), $symbol ? \NumberFormatter::CURRENCY : \NumberFormatter::PATTERN_DECIMAL); $value = $formatter->formatCurrency($value, $valueCurrency); if (!$decimal) { $value = preg_replace('/[.,]00((?=\\D)|$)/', '', $value); } $value = str_replace(array('EU', 'UK', 'US'), '', $value); return $value; }
public function format($config = array()) { $translator = $this->getObject('translator'); $config = new KObjectConfig($config); $config->append(array('format' => JFactory::getLanguage()->getTag(), 'currency_code' => 'USD')); if (class_exists('NumberFormatter')) { $formatter = new \NumberFormatter($config->format, \NumberFormatter::CURRENCY); $result = $formatter->formatCurrency($config->number, $config->currency_code); } else { $result = number_format($config->number, 2, $translator->translate('DECIMALS_SEPARATOR'), $translator->translate('THOUSANDS_SEPARATOR')) . ' ' . $config->currency_code; } return $result; }
/** * Format the money amount to nice display form. * * @param int $amount * @param string|null $currency * @param bool $decimal * @param string|null $locale * * @return string * * @throws \InvalidArgumentException */ public function formatAmount($amount, $currency = null, $decimal = false, $locale = null) { $locale = $locale ?: $this->getDefaultLocale(); $currency = $currency ?: $this->getDefaultCurrency(); if ($decimal) { $formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); } else { $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); } $result = $formatter->formatCurrency($amount / 100, $currency); if (false === $result) { throw new \InvalidArgumentException(sprintf('The amount "%s" of type %s cannot be formatted to currency "%s".', $amount, gettype($amount), $currency)); } return $result; }
/** * @param string $currency This can be any 3 letter ISO 4217 code. You * can also set this to false to return the general currency symbol. * * @return string Currency sign */ public function getCurrencySign($currency = false) { $locale = \Locale::getDefault(); $format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $pattern = $format->formatCurrency('123', $currency); preg_match('/^([^\\s\\xc2\\xa0]*)[\\s\\xc2\\xa0]*123(?:[,.]0+)?[\\s\\xc2\\xa0]*([^\\s\\xc2\\xa0]*)$/u', $pattern, $matches); if (!empty($matches[1])) { $currency_sign = $matches[1]; } elseif (!empty($matches[2])) { $currency_sign = $matches[2]; } else { $currency_sign = '¤'; } return $currency_sign; }
public static function moneda($entero, $localidad = null, $codigo = null) { if (empty($entero)) { return $entero; } $localidad = empty($localidad) ? self::LOCALIDAD_RD : $localidad; $codigo = empty($codigo) ? self::CODIGO_RD : $codigo; $sufijo = $localidad == 'es_DO' ? 'RD' : ''; $fmt = new NumberFormatter($localidad, NumberFormatter::CURRENCY); //$fmt->setTextAttribute(NumberFormatter::CURRENCY_CODE, 'DOP'); $fmt->setPattern(str_replace('¤#', '¤ #', $fmt->getPattern())); return $sufijo . $fmt->formatCurrency($entero, $codigo); //setlocale(LC_MONETARY, $localidad); //return money_format('%i', $entero) . "\n"; }
/** * convert number expressions to value * * @assert ("34000", 'en') == "$34,000" * @assert ("123456.789", 'en') == "$123,457" * @assert ("1234567890", 'en') == "$1,234,567,890" */ public static function format($value, $locale = "en") { switch ($locale) { case 'ja': return self::formatInJapanese($value); break; case 'en': default: if ($locale = 'en') { $locale = 'en-US'; } $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); // 'en' can not omit decimal places even with this, so set 'en-US' $numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0); return $numberFormatter->formatCurrency($value, 'USD'); } }
public function getDisplayedValue($value) { if ($value !== null && $value !== '') { $formatter = new \NumberFormatter($this->locale, $this->style); if ($this->precision !== null) { $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); } if ($this->ruleSet !== null) { $formatter->setTextAttribute(\NumberFormatter::DEFAULT_RULESET, $this->ruleSet); } if ($this->maxFractionDigits !== null) { $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $this->maxFractionDigits); } $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping); if ($this->style === \NumberFormatter::PERCENT && !$this->fractional) { $value /= 100; } if ($this->style === \NumberFormatter::CURRENCY) { if ($this->currencyCode === null) { $this->currencyCode = $formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE); } if (strlen($this->currencyCode) !== 3) { throw new TransformationFailedException('Your locale definition is not complete, you have to define a language and a country. (.e.g en_US, fr_FR)'); } $value = $formatter->formatCurrency($value, $this->currencyCode); } else { $value = $formatter->format($value); } if (intl_is_failure($formatter->getErrorCode())) { throw new TransformationFailedException($formatter->getErrorMessage()); } if (key_exists((string) $value, $this->values)) { $value = $this->values[$value]; } return $value; } return ''; }
function formatCurrency($price) { $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); $formatted = $formatter->formatCurrency($price, 'USD'); return $formatted; }
echo Divipola::findOne(['id_dpto' => $item->id_departamento])->dpto; ?> </td> <td> <?php echo Divipola::findOne(['id' => $item->id_municipio])->mpio; ?> </td> <td> <?php echo $item->descripcion_veredas; ?> </td> <td style="text-align: right;"> <?php echo $formatter->formatCurrency($item->total * 1000, 'COP'); ?> </td> <td style="text-align: right;"> <?php echo $formatter->formatCurrency($item->cofinanciacion * 1000, 'COP'); ?> </td> <td style="text-align: right;"> <?php echo $formatter->formatCurrency($delta * 1000, 'COP'); ?> </td> <td style="text-align: right;"> <?php echo $item->usuarios_existentes;
/** * This method generates an amount using symbol or code of the currency. * * @param mixed $value This is a value used in the amount string. This can be float, integer,... * * <code> * $currencyId = 1; * * $currency = CrowdFundingCurrency::getInstance(JFactory::getDbo(), $currencyId); * * // Return $100 or 100USD. * echo $currency->getAmountString(100); * </code> * * @return string */ public function getAmountString($value) { $intl = (bool) $this->options->get("intl", false); $format = $this->options->get("format"); if (!$intl and !empty($format)) { $value = $this->formatAmount($value); } // 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); $amount = $numberFormat->formatCurrency($value, $this->abbr); } else { // Generate a custom currency string. if (!empty($this->symbol)) { // Symbol if (0 == $this->position) { // Symbol at beginning. $amount = $this->symbol . $value; } else { // Symbol at end. $amount = $value . $this->symbol; } } else { // Code $amount = $value . $this->abbr; } } return $amount; }
protected function formatPrices(&$items, &$total) { $formatter = new \NumberFormatter('en_GB', \NumberFormatter::CURRENCY); foreach ($items as $rowId => $item) { $items[$rowId]['price'] = $formatter->formatCurrency($item['price'], 'GBP'); } $total = $formatter->formatCurrency($total, 'GBP'); }
/** * @param $number * @param string $symbol * @return mixed */ public function formatCurrency($number, $symbol = '') { $fmt = new \NumberFormatter($this->locale->getLocale()->locale, \NumberFormatter::CURRENCY); return $fmt->formatCurrency($number, $symbol); }
public function toStringByLocale($locale, Money $money) { $number_formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); return $number_formatter->formatCurrency($this->getAmountInBaseUnits($money), $money->getCurrency()->getName()); }
/** * 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; }