Permalink
Browse files

Adds option to multiply decimal percentages. Fixes #3814

  • Loading branch information...
1 parent 4ded269 commit e35bd80dfbe4f0634da252f61783521d64437c9a @Phally Phally committed Jul 9, 2013
View
24 lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -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);
}
/**
View
11 lib/Cake/Utility/CakeNumber.php
@@ -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) . '%';
}
View
5 lib/Cake/View/Helper/NumberHelper.php
@@ -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);
}
/**

0 comments on commit e35bd80

Please sign in to comment.