Skip to content

Commit

Permalink
Starting to use NumberFormatter in the Number lib
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 31, 2014
1 parent 0abef1a commit 335342f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/Utility/Number.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Utility;

use Cake\Error\Exception;
use NumberFormatter;

/**
* Number helper library.
Expand Down Expand Up @@ -98,7 +99,9 @@ class Number {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
*/
public static function precision($value, $precision = 3) {
return sprintf("%01.{$precision}f", $value);
$formatter = new NumberFormatter(ini_get('intl.default_locale'), NumberFormatter::DECIMAL);
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $precision);
return $formatter->format($value);
}

/**
Expand All @@ -119,7 +122,7 @@ public static function toReadableSize($size) {
case round($size / 1024 / 1024 / 1024, 2) < 1024:
return __d('cake', '{0,number,#,###.##} GB', $size / 1024 / 1024 / 1024);
default:
return __d('cake', '{0,number} TB', $size / 1024 / 1024 / 1024 / 1024);
return __d('cake', '{0,number,#,###.##} TB', $size / 1024 / 1024 / 1024 / 1024);
}
}

Expand Down
18 changes: 6 additions & 12 deletions tests/TestCase/Utility/NumberTest.php
Expand Up @@ -33,6 +33,7 @@ class NumberTest extends TestCase {
public function setUp() {
parent::setUp();
$this->Number = new Number();
$this->locale = ini_get('intl.default_locale');
}

/**
Expand All @@ -43,6 +44,7 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
unset($this->Number);
ini_set('intl.default_locale', $this->locale);
}

/**
Expand Down Expand Up @@ -669,16 +671,12 @@ public function testToReadableSize() {
* @return void
*/
public function testReadableSizeLocalized() {
$restore = setlocale(LC_NUMERIC, 0);

$this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");

ini_set('intl.default_locale', 'fr_FR');
$result = $this->Number->toReadableSize(1321205);
$this->assertEquals('1,26 MB', $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 512);
$this->assertEquals('512,00 GB', $result);
setlocale(LC_NUMERIC, $restore);
$result = $this->Number->toReadableSize(512.05 * 1024 * 1024 * 1024);
$this->assertEquals('512,05 GB', $result);
}

/**
Expand All @@ -687,13 +685,9 @@ public function testReadableSizeLocalized() {
* @return void
*/
public function testPrecisionLocalized() {
$restore = setlocale(LC_NUMERIC, 0);

$this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");

ini_set('intl.default_locale', 'fr_FR');
$result = $this->Number->precision(1.234);
$this->assertEquals('1,234', $result);
setlocale(LC_NUMERIC, $restore);
}

/**
Expand Down

0 comments on commit 335342f

Please sign in to comment.