Skip to content

Commit

Permalink
Simplify Number::format()
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Apr 9, 2014
1 parent 84c03dc commit 2b5c5b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
46 changes: 16 additions & 30 deletions src/Utility/Number.php
Expand Up @@ -188,39 +188,25 @@ public static function toPercentage($value, $precision = 2, array $options = arr
/**
* Formats a number into a currency format.
*
* @param float $value A floating point number
* @param integer $options If integer then places, if string then before, if (,.-) then use it
* or array with places and before keys
* Options:
*
* - `places` - Number of decimal places to use. ie. 2
* - `before` - The string to place before whole numbers. ie. '['
* - `after` - The string to place after decimal numbers. ie. ']'
* - `thousands` - Thousands separator ie. ','
* - `decimals` - Decimal separator symbol ie. '.'
* - `escape` - Set to false to prevent escaping
*
* @param float $value A floating point number.
* @param array $options An array with options.
* @return string formatted number
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
*/
public static function format($value, $options = false) {
$places = 0;
if (is_int($options)) {
$places = $options;
}

$separators = array(',', '.', '-', ':');

$before = $after = null;
if (is_string($options) && !in_array($options, $separators)) {
$before = $options;
}
$thousands = ',';
if (!is_array($options) && in_array($options, $separators)) {
$thousands = $options;
}
$decimals = '.';
if (!is_array($options) && in_array($options, $separators)) {
$decimals = $options;
}

$escape = true;
if (is_array($options)) {
$defaults = array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.');
$options += $defaults;
extract($options);
}
public static function format($value, array $options = []) {
$defaults = array('before' => '', 'after' => '', 'places' => 2,
'thousands' => ',', 'decimals' => '.', 'escape' => true);
$options += $defaults;
extract($options);

$out = $before . number_format($value, $places, $decimals, $thousands) . $after;

Expand Down
28 changes: 15 additions & 13 deletions tests/TestCase/Utility/NumberTest.php
Expand Up @@ -53,34 +53,38 @@ public function tearDown() {
public function testFormat() {
$value = '100100100';

$result = $this->Number->format($value, '#');
$expected = '#100,100,100';
$result = $this->Number->format($value);
$expected = '100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value, array('before' => '#'));
$expected = '#100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value, 3);
$result = $this->Number->format($value, array('places' => 3));
$expected = '100,100,100.000';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value);
$expected = '100,100,100';
$result = $this->Number->format($value, array('thousands' => '-'));
$expected = '100-100-100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value, '-');
$expected = '100-100-100';
$result = $this->Number->format($value, array('decimals' => ',', 'thousands' => '.'));
$expected = '100.100.100,00';
$this->assertEquals($expected, $result);

$value = 0.00001;
$result = $this->Number->format($value, array('places' => 1));
$result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
$expected = '$0.0';
$this->assertEquals($expected, $result);

$value = -0.00001;
$result = $this->Number->format($value, array('places' => 1));
$result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
$expected = '$-0.0';
$this->assertEquals($expected, $result);

$value = 1.23;
$options = array('decimals' => ',', 'thousands' => '.', 'before' => '', 'after' => ' €');
$options = array('decimals' => ',', 'thousands' => '.', 'after' => ' €');
$result = $this->Number->format($value, $options);
$expected = '1,23 €';
$this->assertEquals($expected, $result);
Expand Down Expand Up @@ -142,7 +146,6 @@ public function testMultibyteFormat() {
'decimals' => '&',
'places' => 3,
'escape' => false,
'before' => '',
));
$expected = '5 199 100&001';
$this->assertEquals($expected, $result);
Expand All @@ -153,7 +156,7 @@ public function testMultibyteFormat() {
'decimals' => '.a',
'escape' => false,
));
$expected = '$1,,000.a45';
$expected = '1,,000.a45';
$this->assertEquals($expected, $result);

$value = 519919827593784.00;
Expand All @@ -172,7 +175,6 @@ public function testMultibyteFormat() {
$result = Number::format($value, array(
'thousands' => '- |-| /-\ >< () |2 -',
'decimals' => '- £€€† -',
'before' => ''
));
$expected = '13- |-| /-\ &gt;&lt; () |2 -371- |-| /-\ &gt;&lt; () |2 -337- £€€† -13';
$this->assertEquals($expected, $result);
Expand Down

0 comments on commit 2b5c5b9

Please sign in to comment.