/** * Formats the number for a certain pattern. The valid patterns are * 'c', 'd', 'e', 'p' or a custom pattern, such as "#.000" for * 3 decimal places. * * @param mixed $number the number to format. * @param string $pattern the format pattern, either, 'c', 'd', 'e', 'p' * or a custom pattern. E.g. "#.000" will format the number to * 3 decimal places. * @param string $currency 3-letter ISO 4217 code. For example, the code * "USD" represents the US Dollar and "EUR" represents the Euro currency. * @param string $charset The charset * @return string formatted number string */ function format($number, $pattern = 'd', $currency = 'USD', $charset = 'UTF-8') { $this->setPattern($pattern); if (strtolower($pattern) == 'p') { $number = $number * 100; } // avoid conversion with exponents // see http://trac.symfony-project.org/ticket/5715 $precision = ini_set('precision', 14); $string = $this->fixFloat($number); ini_set('precision', $precision); $decimal = $this->formatDecimal($string); $integer = $this->formatInteger($this->fixFloat(abs($number))); $result = strlen($decimal) > 0 ? $integer . $decimal : $integer; // get the suffix if ($number >= 0) { $suffix = $this->formatInfo->PositivePattern; } else { if ($number < 0) { $suffix = $this->formatInfo->NegativePattern; } else { $suffix = array('', ''); } } // append and prepend suffix $result = $suffix[0] . $result . $suffix[1]; // replace currency sign $symbol = @$this->formatInfo->getCurrencySymbol($currency); if (null === $symbol) { $symbol = $currency; } $result = str_replace('¤', $symbol, $result); return sfToolkit::I18N_toEncoding($result, $charset); }
/** * For the number for a certain pattern. The valid patterns are * 'c', 'd', 'e', 'p' or a custom pattern, such as "#.000" for * 3 decimal places. * @param mixed the number to format. * @param string the format pattern, either, 'c', 'd', 'e', 'p' * or a custom pattern. E.g. "#.000" will format the number to * 3 decimal places. * @param string 3-letter ISO 4217 code. For example, the code * "USD" represents the US Dollar and "EUR" represents the Euro currency. * @return string formatted number string */ function format($number, $pattern = 'd', $currency = 'USD', $charset = 'UTF-8') { $this->setPattern($pattern); if (strtolower($pattern) == 'p') { $number = $number * 100; } $string = (string) $number; $decimal = $this->formatDecimal($string); $integer = $this->formatInteger(abs($number)); if (strlen($decimal) > 0) { $result = $integer . $decimal; } else { $result = $integer; } //get the suffix if ($number >= 0) { $suffix = $this->formatInfo->PositivePattern; } else { if ($number < 0) { $suffix = $this->formatInfo->NegativePattern; } else { $suffix = array("", ""); } } //append and prepend suffix $result = $suffix[0] . $result . $suffix[1]; //replace currency sign $symbol = @$this->formatInfo->getCurrencySymbol($currency); if ($symbol === null) { $symbol = $currency; } $result = str_replace('¤', $symbol, $result); return I18N_toEncoding($result, $charset); }