Browse Source

Added support for locale-dependent decimal and grouping separators.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
cbf642d931
  1. 18
      framework/yii/base/Formatter.php
  2. 20
      framework/yii/i18n/Formatter.php

18
framework/yii/base/Formatter.php

@ -39,17 +39,19 @@ class Formatter extends Component
public $datetimeFormat = 'Y/m/d h:i:s A';
/**
* @var array the text to be displayed when formatting a boolean value. The first element corresponds
* to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
* to the text display for false, the second element for true. Defaults to `array('No', 'Yes')`.
*/
public $booleanFormat;
/**
* @var string the character displayed as the decimal point when formatting a number.
* If not set, "." will be used.
*/
public $decimalSeparator = '.';
public $decimalSeparator;
/**
* @var string the character displayed as the thousands separator character when formatting a number.
* If not set, "," will be used.
*/
public $thousandSeparator = ',';
public $thousandSeparator;
/**
@ -273,7 +275,11 @@ class Formatter extends Component
*/
public function asDouble($value, $decimals = 2)
{
return str_replace('.', $this->decimalSeparator, sprintf("%.{$decimals}f", $value));
if ($this->decimalSeparator === null) {
return sprintf("%.{$decimals}f", $value);
} else {
return str_replace('.', $this->decimalSeparator, sprintf("%.{$decimals}f", $value));
}
}
/**
@ -287,6 +293,8 @@ class Formatter extends Component
*/
public function asNumber($value, $decimals = 0)
{
return number_format($value, $decimals, $this->decimalSeparator, $this->thousandSeparator);
$ds = isset($this->decimalSeparator) ? $this->decimalSeparator: '.';
$ts = isset($this->thousandSeparator) ? $this->thousandSeparator: ',';
return number_format($value, $decimals, $ds, $ts);
}
}

20
framework/yii/i18n/Formatter.php

@ -54,6 +54,16 @@ class Formatter extends \yii\base\Formatter
* creating a new number formatter to format decimals, currencies, etc.
*/
public $numberFormatOptions = array();
/**
* @var string the character displayed as the decimal point when formatting a number.
* If not set, the decimal separator corresponding to [[locale]] will be used.
*/
public $decimalSeparator;
/**
* @var string the character displayed as the thousands separator character when formatting a number.
* If not set, the thousand separator corresponding to [[locale]] will be used.
*/
public $thousandSeparator;
/**
@ -70,6 +80,16 @@ class Formatter extends \yii\base\Formatter
if ($this->locale === null) {
$this->locale = Yii::$app->language;
}
if ($this->decimalSeparator === null || $this->thousandSeparator === null) {
$formatter = new NumberFormatter($this->locale, NumberFormatter::DECIMAL);
if ($this->decimalSeparator === null) {
$this->decimalSeparator = $formatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
}
if ($this->thousandSeparator === null) {
$this->thousandSeparator = $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
}
}
parent::init();
}

Loading…
Cancel
Save