Browse Source

refactored numberformatting in base\Formatter

added decimalSeparator and thousandSeparator properties.
issue #48
tags/2.0.0-beta
Carsten Brandt 12 years ago
parent
commit
8b6447876a
  1. 20
      framework/yii/base/Formatter.php
  2. 23
      tests/unit/framework/base/FormatterTest.php

20
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 <code>array('No', 'Yes')</code>. * to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
*/ */
public $booleanFormat; 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. * 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 mixed $value the value to be formatted
* @param integer $decimals the number of digits after the decimal point * @param integer $decimals the number of digits after the decimal point
* @return string the formatting result. * @return string the formatting result.
* @see decimalSeparator
*/ */
public function asDouble($value, $decimals = 2) 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. * This method calls the PHP number_format() function to do the formatting.
* @param mixed $value the value to be formatted * @param mixed $value the value to be formatted
* @param integer $decimals the number of digits after the decimal point * @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 * @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);
} }
} }

23
tests/unit/framework/base/FormatterTest.php

@ -140,6 +140,10 @@ class FormatterTest extends TestCase
$this->assertSame("123", $this->formatter->asInteger($value)); $this->assertSame("123", $this->formatter->asInteger($value));
$value = 'a'; $value = 'a';
$this->assertSame("0", $this->formatter->asInteger($value)); $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() public function testAsDouble()
@ -147,12 +151,29 @@ class FormatterTest extends TestCase
$value = 123.12; $value = 123.12;
$this->assertSame("123.12", $this->formatter->asDouble($value)); $this->assertSame("123.12", $this->formatter->asDouble($value));
$this->assertSame("123.1", $this->formatter->asDouble($value, 1)); $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() public function testAsNumber()
{ {
$value = 123123.123; $value = 123123.123;
$this->assertSame("123,123", $this->formatter->asNumber($value)); $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));
} }
} }

Loading…
Cancel
Save