Fixing NumberHelper code formatting, and adding currency() method (Ticket #853)

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3124 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-06-17 03:19:43 +00:00
parent 2db82c2b30
commit 5de968246f

View file

@ -36,7 +36,7 @@
* @package cake * @package cake
* @subpackage cake.cake.libs.view.helpers * @subpackage cake.cake.libs.view.helpers
*/ */
class NumberHelper extends Helper{ class NumberHelper extends Helper {
/** /**
* Formats a number with a level of precision. * Formats a number with a level of precision.
* *
@ -45,9 +45,9 @@ class NumberHelper extends Helper{
* @return float Enter description here... * @return float Enter description here...
* @static * @static
*/ */
function precision($number, $precision = 3) { function precision($number, $precision = 3) {
return sprintf("%01.{$precision}f", $number); return sprintf("%01.{$precision}f", $number);
} }
/** /**
* Returns a formatted-for-humans file size. * Returns a formatted-for-humans file size.
@ -56,25 +56,22 @@ class NumberHelper extends Helper{
* @return string Human readable size * @return string Human readable size
* @static * @static
*/ */
function toReadableSize($size) { function toReadableSize($size) {
switch($size) switch($size) {
{ case 1:
case 1: return '1 Byte'; return '1 Byte';
case $size < 1024:
case $size < 1024: return $size . ' Bytes'; return $size . ' Bytes';
case $size < 1024 * 1024:
case $size < 1024 * 1024: return NumberHelper::precision($size / 1024, 0) . ' KB'; return NumberHelper::precision($size / 1024, 0) . ' KB';
case $size < 1024 * 1024 * 1024:
case $size < 1024 * 1024 * 1024: return NumberHelper::precision($size / 1024 / 1024, 2) . ' MB'; return NumberHelper::precision($size / 1024 / 1024, 2) . ' MB';
case $size < 1024 * 1024 * 1024 * 1024:
case $size < 1024 * 1024 * 1024 * 1024: return NumberHelper::precision($size / 1024 / 1024 / 1024, 2) . ' GB';
return NumberHelper::precision($size / 1024 / 1024 / 1024, 2) . ' GB'; case $size < 1024 * 1024 * 1024 * 1024 * 1024:
return NumberHelper::precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB';
case $size < 1024 * 1024 * 1024 * 1024 * 1024: }
return NumberHelper::precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB'; }
}
}
/** /**
* Formats a number into a percentage string. * Formats a number into a percentage string.
* *
@ -83,8 +80,32 @@ class NumberHelper extends Helper{
* @return string Percentage string * @return string Percentage string
* @static * @static
*/ */
function toPercentage($number, $precision = 2) { function toPercentage($number, $precision = 2) {
return NumberHelper::precision($number, $precision) . '%'; return NumberHelper::precision($number, $precision) . '%';
} }
/**
* Formats a number into a percentage string.
*
* @param float $number A floating point number
* @param integer $precision The precision of the returned number
* @return string Percentage string
* @static
*/
function currency ($number, $currency = 'USD') {
switch ($currency) {
case "EUR":
$return = "&#128; " . number_format ($number, 2, ",", ".");
break;
case "GBP":
$return = "&#163;" . number_format ($number, 2, ".", ",");
break;
case 'USD':
default:
$return = "$" . number_format ($number, 2, ".", ",");
break;
}
return $return;
}
} }
?> ?>