Skip to content

Commit e35bd80

Browse files
committed
Adds option to multiply decimal percentages. Fixes #3814
1 parent 4ded269 commit e35bd80

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

lib/Cake/Test/Case/Utility/CakeNumberTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,30 @@ public function testToPercentage() {
650650
$result = $this->Number->toPercentage(0, 4);
651651
$expected = '0.0000%';
652652
$this->assertEquals($expected, $result);
653+
654+
$result = $this->Number->toPercentage(45, 0, array('multiply' => false));
655+
$expected = '45%';
656+
$this->assertEquals($expected, $result);
657+
658+
$result = $this->Number->toPercentage(45, 2, array('multiply' => false));
659+
$expected = '45.00%';
660+
$this->assertEquals($expected, $result);
661+
662+
$result = $this->Number->toPercentage(0, 0, array('multiply' => false));
663+
$expected = '0%';
664+
$this->assertEquals($expected, $result);
665+
666+
$result = $this->Number->toPercentage(0, 4, array('multiply' => false));
667+
$expected = '0.0000%';
668+
$this->assertEquals($expected, $result);
669+
670+
$result = $this->Number->toPercentage(0.456, 0, array('multiply' => true));
671+
$expected = '46%';
672+
$this->assertEquals($expected, $result);
673+
674+
$result = $this->Number->toPercentage(0.456, 2, array('multiply' => true));
675+
$expected = '45.60%';
676+
$this->assertEquals($expected, $result);
653677
}
654678

655679
/**

lib/Cake/Utility/CakeNumber.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,21 @@ public static function fromReadableSize($size, $default = false) {
160160
/**
161161
* Formats a number into a percentage string.
162162
*
163+
* Options:
164+
*
165+
* - `multiply`: Multiply the input value by 100 for decimal percentages.
166+
*
163167
* @param float $value A floating point number
164168
* @param integer $precision The precision of the returned number
169+
* @param array $options Options
165170
* @return string Percentage string
166171
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
167172
*/
168-
public static function toPercentage($value, $precision = 2) {
173+
public static function toPercentage($value, $precision = 2, $options = array()) {
174+
$options += array('multiply' => false);
175+
if ($options['multiply']) {
176+
$value *= 100;
177+
}
169178
return self::precision($value, $precision) . '%';
170179
}
171180

lib/Cake/View/Helper/NumberHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ public function toReadableSize($size) {
101101
*
102102
* @param float $number A floating point number
103103
* @param integer $precision The precision of the returned number
104+
* @param array $options Options
104105
* @return string Percentage string
105106
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
106107
*/
107-
public function toPercentage($number, $precision = 2) {
108-
return $this->_engine->toPercentage($number, $precision);
108+
public function toPercentage($number, $precision = 2, $options = array()) {
109+
return $this->_engine->toPercentage($number, $precision, $options);
109110
}
110111

111112
/**

0 commit comments

Comments
 (0)