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
This commit is contained in:
gwoo 2007-05-24 20:45:27 +00:00
parent 89c2ce23b0
commit 75f04aebd9
2 changed files with 138 additions and 10 deletions

View file

@ -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 = "&#128; " . number_format ($number, 2, ",", ".");
return $this->format($number, array('places'=>'2', 'before'=>"&#128"));
break;
case "GBP":
$return = "&#163;" . number_format ($number, 2, ".", ",");
return $this->format($number, array('places'=>'2', 'before'=>"&#163"));
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;
}
}

View file

@ -0,0 +1,81 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* 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);
}
}
?>