mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-18 07:20:18 +08:00
Added support for locale-dependent decimal and grouping separators.
This commit is contained in:
@@ -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,8 +275,12 @@ class Formatter extends Component
|
||||
*/
|
||||
public function asDouble($value, $decimals = 2)
|
||||
{
|
||||
if ($this->decimalSeparator === null) {
|
||||
return sprintf("%.{$decimals}f", $value);
|
||||
} else {
|
||||
return str_replace('.', $this->decimalSeparator, sprintf("%.{$decimals}f", $value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as a number with decimal and thousand separators.
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user