Skip to content

Commit

Permalink
Adds option to multiply decimal percentages. Fixes #3814
Browse files Browse the repository at this point in the history
  • Loading branch information
Phally committed Jul 10, 2013
1 parent 4ded269 commit e35bd80
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
24 changes: 24 additions & 0 deletions lib/Cake/Test/Case/Utility/CakeNumberTest.php
Expand Up @@ -650,6 +650,30 @@ public function testToPercentage() {
$result = $this->Number->toPercentage(0, 4);
$expected = '0.0000%';
$this->assertEquals($expected, $result);

$result = $this->Number->toPercentage(45, 0, array('multiply' => false));
$expected = '45%';
$this->assertEquals($expected, $result);

$result = $this->Number->toPercentage(45, 2, array('multiply' => false));
$expected = '45.00%';
$this->assertEquals($expected, $result);

$result = $this->Number->toPercentage(0, 0, array('multiply' => false));
$expected = '0%';
$this->assertEquals($expected, $result);

$result = $this->Number->toPercentage(0, 4, array('multiply' => false));
$expected = '0.0000%';
$this->assertEquals($expected, $result);

$result = $this->Number->toPercentage(0.456, 0, array('multiply' => true));
$expected = '46%';
$this->assertEquals($expected, $result);

$result = $this->Number->toPercentage(0.456, 2, array('multiply' => true));
$expected = '45.60%';
$this->assertEquals($expected, $result);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/Cake/Utility/CakeNumber.php
Expand Up @@ -160,12 +160,21 @@ public static function fromReadableSize($size, $default = false) {
/**
* Formats a number into a percentage string.
*
* Options:
*
* - `multiply`: Multiply the input value by 100 for decimal percentages.
*
* @param float $value A floating point number
* @param integer $precision The precision of the returned number
* @param array $options Options
* @return string Percentage string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
*/
public static function toPercentage($value, $precision = 2) {
public static function toPercentage($value, $precision = 2, $options = array()) {
$options += array('multiply' => false);
if ($options['multiply']) {
$value *= 100;
}
return self::precision($value, $precision) . '%';
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/View/Helper/NumberHelper.php
Expand Up @@ -101,11 +101,12 @@ public function toReadableSize($size) {
*
* @param float $number A floating point number
* @param integer $precision The precision of the returned number
* @param array $options Options
* @return string Percentage string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
*/
public function toPercentage($number, $precision = 2) {
return $this->_engine->toPercentage($number, $precision);
public function toPercentage($number, $precision = 2, $options = array()) {
return $this->_engine->toPercentage($number, $precision, $options);
}

/**
Expand Down

0 comments on commit e35bd80

Please sign in to comment.