Skip to content

Commit

Permalink
Allow toPercentage to localize output.
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Nov 17, 2014
1 parent 3d5e4aa commit 13cadcc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/I18n/Number.php
Expand Up @@ -42,13 +42,18 @@ class Number {
/**
* Formats a number with a level of precision.
*
* Options:
*
* - `locale`: The locale name to use for formatting the number, e.g. fr_FR
*
* @param float $value A floating point number.
* @param int $precision The precision of the returned number.
* @param array $options Additional options
* @return string Formatted float.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
*/
public static function precision($value, $precision = 3) {
$formatter = static::formatter(['precision' => $precision, 'places' => $precision]);
public static function precision($value, $precision = 3, array $options = array()) {
$formatter = static::formatter(['precision' => $precision, 'places' => $precision] + $options);
return $formatter->format($value);
}

Expand Down Expand Up @@ -80,6 +85,7 @@ public static function toReadableSize($size) {
* Options:
*
* - `multiply`: Multiply the input value by 100 for decimal percentages.
* - `locale`: The locale name to use for formatting the number, e.g. fr_FR
*
* @param float $value A floating point number
* @param int $precision The precision of the returned number
Expand All @@ -92,7 +98,7 @@ public static function toPercentage($value, $precision = 2, array $options = arr
if ($options['multiply']) {
$value *= 100;
}
return static::precision($value, $precision) . '%';
return static::precision($value, $precision, $options) . '%';
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/I18n/NumberTest.php
Expand Up @@ -411,6 +411,10 @@ public function testToPercentage() {
$result = $this->Number->toPercentage(0.456, 2, array('multiply' => true));
$expected = '45.60%';
$this->assertEquals($expected, $result);

$result = $this->Number->toPercentage(0.456, 2, array('locale' => 'de-DE'));
$expected = '45,60%';
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit 13cadcc

Please sign in to comment.