From 75f04aebd9a82a3331230a874cfdbc7887eb17c2 Mon Sep 17 00:00:00 2001 From: gwoo Date: Thu, 24 May 2007 20:45:27 +0000 Subject: [PATCH] adding number helper format and test for format and currency git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5173 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/number.php | 67 ++++++++++++--- .../cases/libs/view/helpers/number.test.php | 81 +++++++++++++++++++ 2 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 cake/tests/cases/libs/view/helpers/number.test.php diff --git a/cake/libs/view/helpers/number.php b/cake/libs/view/helpers/number.php index e991e6c9b..5d08e05ea 100644 --- a/cake/libs/view/helpers/number.php +++ b/cake/libs/view/helpers/number.php @@ -63,13 +63,13 @@ class NumberHelper extends AppHelper { case $size < 1024: return $size . ' Bytes'; case $size < 1024 * 1024: - return NumberHelper::precision($size / 1024, 0) . ' KB'; + return $this->precision($size / 1024, 0) . ' KB'; case $size < 1024 * 1024 * 1024: - return NumberHelper::precision($size / 1024 / 1024, 2) . ' MB'; + return $this->precision($size / 1024 / 1024, 2) . ' MB'; case $size < 1024 * 1024 * 1024 * 1024: - return NumberHelper::precision($size / 1024 / 1024 / 1024, 2) . ' GB'; + return $this->precision($size / 1024 / 1024 / 1024, 2) . ' GB'; case $size < 1024 * 1024 * 1024 * 1024 * 1024: - return NumberHelper::precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB'; + return $this->precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB'; } } /** @@ -81,10 +81,55 @@ class NumberHelper extends AppHelper { * @static */ function toPercentage($number, $precision = 2) { - return NumberHelper::precision($number, $precision) . '%'; + return $this->precision($number, $precision) . '%'; } /** - * Formats a number into a percentage string. + * Formats a number into a currnecy format. + * + * @param float $number A floating point number + * @param integer $options if int then places, if string then before, if (,.-) then use it + * or array with places and before keys + * @return string formatted number + * @static + */ + function format($number, $options = false) { + $places = 0; + if(is_int($options)) { + $places = $options; + } + + $seperators = array(',', '.', '-', ':'); + + $before = null; + if(is_string($options) && !in_array( $options, $seperators)) { + $before = $options; + } + $seperator = ','; + if(!is_array($options) && in_array( $options, $seperators)) { + $seperator = $options; + } + + if(is_array($options)) { + if(isset($options['places'])) { + $places = $options['places']; + unset($options['places']); + } + + if(isset($options['before'])) { + $before = $options['before']; + unset($options['before']); + } + + if(isset($options['seperator'])) { + $seperator = $options['seperator']; + unset($options['seperator']); + } + } + + return h($before) . number_format ($number, $places, ".", $seperator); + } +/** + * Formats a number into a currency format. * * @param float $number A floating point number * @param integer $precision The precision of the returned number @@ -92,19 +137,21 @@ class NumberHelper extends AppHelper { * @static */ function currency ($number, $currency = 'USD') { + switch ($currency) { case "EUR": - $return = "€ " . number_format ($number, 2, ",", "."); + return $this->format($number, array('places'=>'2', 'before'=>"€")); break; case "GBP": - $return = "£" . number_format ($number, 2, ".", ","); + return $this->format($number, array('places'=>'2', 'before'=>"£")); break; case 'USD': + return $this->format($number, array('places'=>'2', 'before'=>"$")); + break; default: - $return = "$" . number_format ($number, 2, ".", ","); + return $this->format($number, array('places'=>'2', 'before'=> $currency)); break; } - return $return; } } diff --git a/cake/tests/cases/libs/view/helpers/number.test.php b/cake/tests/cases/libs/view/helpers/number.test.php new file mode 100644 index 000000000..a87d8b4c6 --- /dev/null +++ b/cake/tests/cases/libs/view/helpers/number.test.php @@ -0,0 +1,81 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.cases.libs.view.helpers + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ + require_once LIBS.'../app_helper.php'; + require_once LIBS.DS.'view'.DS.'helper.php'; + require_once LIBS.DS.'view'.DS.'helpers'.DS.'number.php'; +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.view.helpers + */ +class NumberTest extends UnitTestCase { + var $helper = null; + + + function setUp() { + $this->Helper =& new NumberHelper(); + } + + function testFormatAndCurrency() { + $value = '1001001001'; + + $result = $this->Helper->format($value, '#'); + $expected = '#100,100,100'; + $this->assertEqual($expected, $result); + + $result = $this->Helper->format($value); + $expected = '100,100,100'; + $this->assertEqual($expected, $result); + + $result = $this->Helper->format($value, '-'); + $expected = '100-100-100'; + $this->assertEqual($expected, $result); + + $result = $this->Helper->currency($value); + $expected = '$100,100,100.00'; + $this->assertEqual($expected, $result); + + $result = $this->Helper->currency($value, '#'); + $expected = '#100,100,100.00'; + $this->assertEqual($expected, $result); + + $result = $this->Helper->currency($value, false); + $expected = '100,100,100.00'; + $this->assertEqual($expected, $result); + + + } + + function tearDown() { + unset($this->Helper); + } +} + +?> \ No newline at end of file