From 559fb5805ba1ec0ac60ec2afce920e3e87514b14 Mon Sep 17 00:00:00 2001 From: Mark van Driel Date: Wed, 14 Aug 2013 16:00:10 +0200 Subject: [PATCH] Fix formatting with locales using , as decimal separator No double number formatting for readable size Fixed testPrecisionLocalized for travis (when locale is missing?) Switched locale to nl_NL and fixed test Skip test when locale is not available --- lib/Cake/Test/Case/Utility/CakeNumberTest.php | 15 +++++++++++++++ lib/Cake/Utility/CakeNumber.php | 10 +++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeNumberTest.php b/lib/Cake/Test/Case/Utility/CakeNumberTest.php index 2635a754b..0259c8485 100644 --- a/lib/Cake/Test/Case/Utility/CakeNumberTest.php +++ b/lib/Cake/Test/Case/Utility/CakeNumberTest.php @@ -596,6 +596,21 @@ class CakeNumberTest extends CakeTestCase { setlocale(LC_NUMERIC, $restore); } +/** + * test precision() with locales + * + * @return void + */ + public function testPrecisionLocalized() { + $restore = setlocale(LC_NUMERIC, 0); + + $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available."); + + $result = $this->Number->precision(1.234); + $this->assertEquals('1,234', $result); + setlocale(LC_NUMERIC, $restore); + } + /** * testToPercentage method * diff --git a/lib/Cake/Utility/CakeNumber.php b/lib/Cake/Utility/CakeNumber.php index 95bc5231c..7d12dca03 100644 --- a/lib/Cake/Utility/CakeNumber.php +++ b/lib/Cake/Utility/CakeNumber.php @@ -84,7 +84,7 @@ class CakeNumber { * @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); + return sprintf("%01.{$precision}f", $value); } /** @@ -99,13 +99,13 @@ class CakeNumber { case $size < 1024: return __dn('cake', '%d Byte', '%d Bytes', $size, $size); case round($size / 1024) < 1024: - return __d('cake', '%d KB', self::precision($size / 1024, 0)); + return __d('cake', '%s KB', self::precision($size / 1024, 0)); case round($size / 1024 / 1024, 2) < 1024: - return __d('cake', '%.2f MB', self::precision($size / 1024 / 1024, 2)); + return __d('cake', '%s MB', self::precision($size / 1024 / 1024, 2)); case round($size / 1024 / 1024 / 1024, 2) < 1024: - return __d('cake', '%.2f GB', self::precision($size / 1024 / 1024 / 1024, 2)); + return __d('cake', '%s GB', self::precision($size / 1024 / 1024 / 1024, 2)); default: - return __d('cake', '%.2f TB', self::precision($size / 1024 / 1024 / 1024 / 1024, 2)); + return __d('cake', '%s TB', self::precision($size / 1024 / 1024 / 1024 / 1024, 2)); } }