本文整理汇总了PHP中NumberFormatter::setAttribute方法的典型用法代码示例。如果您正苦于以下问题:PHP NumberFormatter::setAttribute方法的具体用法?PHP NumberFormatter::setAttribute怎么用?PHP NumberFormatter::setAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NumberFormatter
的用法示例。
在下文中一共展示了NumberFormatter::setAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: decimalNumber
/**
* Formatação de numero decimal
* @param float/integer $value
* @param integer $precision
* @param string $language
* @return float
*/
public static function decimalNumber($value, $precision = 2, $language = 'pt_BR')
{
$valDecimal = new \NumberFormatter($language, \NumberFormatter::DECIMAL);
$valDecimal->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $precision);
$valDecimal->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $precision);
return $valDecimal->format((double) $value, \NumberFormatter::TYPE_DOUBLE);
}
示例2: getNumberFormatter
/**
* Returns a preconfigured \NumberFormatter instance
*
* @return \NumberFormatter
*/
protected function getNumberFormatter()
{
$formatter = new \NumberFormatter($this->locale, \NumberFormatter::DECIMAL);
if ($this->getOption('precision') !== null) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision'));
}
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->getOption('grouping'));
return $formatter;
}
示例3: render
public function render(\Erebot\IntlInterface $translator)
{
$locale = $translator->getLocale(\Erebot\IntlInterface::LC_NUMERIC);
$formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
$formatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0);
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 100);
$result = (string) $formatter->format($this->value);
return $result;
}
示例4: convertDefaultToLocalizedFromLocale
/**
* {@inheritdoc}
*/
public function convertDefaultToLocalizedFromLocale($number, $locale)
{
if (null === $number || '' === $number) {
return $number;
}
$numberFormatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
if (is_numeric($number) && floor($number) != $number) {
$numberFormatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2);
$numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 4);
}
return $numberFormatter->format($number);
}
示例5: parse_decimals
function parse_decimals($number)
{
// ignore empty strings and return
if (empty($number)) {
return $number;
}
$config = get_instance()->config;
$fmt = new \NumberFormatter($config->item('number_locale'), \NumberFormatter::DECIMAL);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $config->item('quantity_decimals'));
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $config->item('quantity_decimals'));
return $fmt->parse($number);
}
示例6: format
/**
* {@inheritdoc}
*/
public function format($value, array $options = [])
{
if (!is_numeric($value)) {
throw new InvalidTypeException(sprintf('The number formatter expects a numeric value, got "%s".', is_object($value) ? get_class($value) : gettype($value)));
}
$scale = isset($options['scale']) ? $options['scale'] : 2;
$rounding = isset($options['rounding']) ? $options['rounding'] : \NumberFormatter::ROUND_HALFUP;
$grouping = isset($options['grouping']) ? $options['grouping'] : false;
$formatter = new \NumberFormatter($this->localeContext->getLocale(), \NumberFormatter::DECIMAL);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $scale);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $rounding);
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, $grouping);
return str_replace(" ", ' ', $formatter->format($value));
}
示例7: percent
/**
* Il valore viene convertito in percentuale con relativo simbolo
*
* - Se non vengono passati i decimali vengono presentati quelli impostati dal locale
* - Se sono presenti vengono presentati sono quelli indicati es. 2 decimali 10,123 -> 10,12%, 10,456 -> 10,46%
*
* @param $number
* @param null $decimal
* @return bool|string
*/
public function percent($number, $decimal = null)
{
is_null($decimal) ? $decimal = $this->getMaxDigits() : null;
$number = $number / 100;
$nft = new NF($this->locale, NF::PERCENT);
$nft->setAttribute(NF::FRACTION_DIGITS, $decimal);
return $nft->format($number);
}
示例8: getNumberFormatter
/**
* {@inheritDoc}
*/
protected function getNumberFormatter()
{
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
if (null !== $this->precision) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision);
}
return $formatter;
}
示例9: getDefaultNumberFormatter
protected function getDefaultNumberFormatter($currencyCode, $locale = null)
{
if (is_null($locale)) {
$locale = \Locale::getDefault();
}
$numberFormatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$numberFormatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currencyCode);
$numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->decimals);
return $numberFormatter;
}
示例10: format
/**
* {@inheritdoc}
*/
public function format($value)
{
$uiLocale = $this->getUiLocale();
if (null === $uiLocale) {
return $value;
}
$numberFormatter = new \NumberFormatter($uiLocale, \NumberFormatter::DECIMAL);
$numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getDecimalCount($value));
return $numberFormatter->format($value);
}
示例11: format
/**
* {@inheritdoc}
*/
public function format($value, array $options = array())
{
if (null === $value) {
return $options['null_value'];
}
if (!is_numeric($value)) {
throw FormatterException::invalidType($this, $value, 'numeric value');
}
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
if (null !== $options['precision']) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $options['precision']);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $options['rounding_mode']);
}
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, $options['grouping']);
$value = $formatter->format($value);
if (intl_is_failure($formatter->getErrorCode())) {
throw FormatterException::intlError($this, $formatter->getErrorMessage());
}
return $value;
}
示例12: format
/**
* Format the specified number to a string with the specified number of
* fractional digits.
*
* @param
* double d
* The number to format.
* @param
* int digits
* The number of fractional digits.
* @return string The number formatted as a string.
*/
public function format($d, $digits)
{
$this->synchronized(function ($thread) {
if (is_infinite($d) || is_nan($d)) {
return "0";
}
$this->numberFormatter->setAttribute(\NumberFormatter::GROUPING_USED, false);
$this->numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $digits);
return $this->numberFormatter->format($d);
});
}
示例13: check_number_locale
public function check_number_locale()
{
$number_locale = $this->input->post('number_locale');
$fmt = new \NumberFormatter($number_locale, \NumberFormatter::CURRENCY);
$currency_symbol = empty($this->input->post('currency_symbol')) ? $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL) : $this->input->post('currency_symbol');
if ($this->input->post('thousands_separator') == "false") {
$fmt->setAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '');
}
$fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency_symbol);
$number_local_example = $fmt->format(1234567890.123);
echo json_encode(array('success' => $number_local_example != FALSE, 'number_locale_example' => $number_local_example, 'currency_symbol' => $currency_symbol, 'thousands_separator' => $fmt->getAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL) != ''));
}
示例14: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value)) {
throw new UnexpectedTypeException($value, 'string');
}
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_DOWN);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0);
$formatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0);
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0);
$decimalSeparator = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
$position = 0;
$formatter->parse($value, PHP_INT_SIZE == 8 ? $formatter::TYPE_INT64 : $formatter::TYPE_INT32, $position);
if (intl_is_failure($formatter->getErrorCode()) || strpos($value, $decimalSeparator) !== false || $position < strlen($value)) {
/** @var Integer $constraint */
$this->context->addViolation($constraint->message, ['{{ value }}' => $this->formatValue($value)]);
}
}
示例15: getDisplayedValue
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 '';
}