diff --git a/framework/yii/base/Formatter.php b/framework/yii/base/Formatter.php index 8945d74..d15e5f2 100644 --- a/framework/yii/base/Formatter.php +++ b/framework/yii/base/Formatter.php @@ -42,6 +42,14 @@ class Formatter extends Component * 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. + */ + public $decimalSeparator = '.'; + /** + * @var string the character displayed as the thousands separator character when formatting a number. + */ + public $thousandSeparator = ','; /** @@ -257,13 +265,15 @@ class Formatter extends Component /** * Formats the value as a double number. + * Property [[decimalSeparator]] will be used to represent the decimal point. * @param mixed $value the value to be formatted * @param integer $decimals the number of digits after the decimal point * @return string the formatting result. + * @see decimalSeparator */ public function asDouble($value, $decimals = 2) { - return sprintf("%.{$decimals}f", $value); + return str_replace('.', $this->decimalSeparator, sprintf("%.{$decimals}f", $value)); } /** @@ -271,12 +281,12 @@ class Formatter extends Component * This method calls the PHP number_format() function to do the formatting. * @param mixed $value the value to be formatted * @param integer $decimals the number of digits after the decimal point - * @param string $decimalSeparator the character displayed as the decimal point - * @param string $thousandSeparator the character displayed as the thousands separator character. * @return string the formatted result + * @see decimalSeparator + * @see thousandSeparator */ - public function asNumber($value, $decimals = 0 , $decimalSeparator = '.' , $thousandSeparator = ',' ) + public function asNumber($value, $decimals = 0) { - return number_format($value, $decimals, $decimalSeparator, $thousandSeparator); + return number_format($value, $decimals, $this->decimalSeparator, $this->thousandSeparator); } } diff --git a/tests/unit/framework/base/FormatterTest.php b/tests/unit/framework/base/FormatterTest.php index 3e7ac5f..e9a909f 100644 --- a/tests/unit/framework/base/FormatterTest.php +++ b/tests/unit/framework/base/FormatterTest.php @@ -140,6 +140,10 @@ class FormatterTest extends TestCase $this->assertSame("123", $this->formatter->asInteger($value)); $value = 'a'; $this->assertSame("0", $this->formatter->asInteger($value)); + $value = -123.23; + $this->assertSame("-123", $this->formatter->asInteger($value)); + $value = "-123abc"; + $this->assertSame("-123", $this->formatter->asInteger($value)); } public function testAsDouble() @@ -147,12 +151,29 @@ class FormatterTest extends TestCase $value = 123.12; $this->assertSame("123.12", $this->formatter->asDouble($value)); $this->assertSame("123.1", $this->formatter->asDouble($value, 1)); + $this->assertSame("123", $this->formatter->asDouble($value, 0)); + $value = 123; + $this->assertSame("123.00", $this->formatter->asDouble($value)); + $this->formatter->decimalSeparator = ','; + $value = 123.12; + $this->assertSame("123,12", $this->formatter->asDouble($value)); + $this->assertSame("123,1", $this->formatter->asDouble($value, 1)); + $this->assertSame("123", $this->formatter->asDouble($value, 0)); + $value = 123123.123; + $this->assertSame("123123,12", $this->formatter->asDouble($value)); } public function testAsNumber() { $value = 123123.123; $this->assertSame("123,123", $this->formatter->asNumber($value)); - $this->assertSame("123.123,12", $this->formatter->asNumber($value, 2, ',', '.')); + $this->assertSame("123,123.12", $this->formatter->asNumber($value, 2)); + $this->formatter->decimalSeparator = ','; + $this->formatter->thousandSeparator = ' '; + $this->assertSame("123 123", $this->formatter->asNumber($value)); + $this->assertSame("123 123,12", $this->formatter->asNumber($value, 2)); + $this->formatter->thousandSeparator = ''; + $this->assertSame("123123", $this->formatter->asNumber($value)); + $this->assertSame("123123,12", $this->formatter->asNumber($value, 2)); } }