number.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: number.php 8071 2009-03-03 23:39:48Z jperras $ */
00003 /**
00004  * Number Helper.
00005  *
00006  * Methods to make numbers more readable.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00011  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.cake.libs.view.helpers
00021  * @since         CakePHP(tm) v 0.10.0.1076
00022  * @version       $Revision: 8071 $
00023  * @modifiedby    $LastChangedBy: jperras $
00024  * @lastmodified  $Date: 2009-03-03 18:39:48 -0500 (Tue, 03 Mar 2009) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Number helper library.
00029  *
00030  * Methods to make numbers more readable.
00031  *
00032  * @package       cake
00033  * @subpackage    cake.cake.libs.view.helpers
00034  */
00035 class NumberHelper extends AppHelper {
00036 /**
00037  * Formats a number with a level of precision.
00038  *
00039  * @param  float    $number A floating point number.
00040  * @param  integer $precision The precision of the returned number.
00041  * @return float Enter description here...
00042  * @static
00043  */
00044     function precision($number, $precision = 3) {
00045         return sprintf("%01.{$precision}f", $number);
00046     }
00047 /**
00048  * Returns a formatted-for-humans file size.
00049  *
00050  * @param integer $length Size in bytes
00051  * @return string Human readable size
00052  * @static
00053  */
00054     function toReadableSize($size) {
00055         switch (true) {
00056             case $size < 1024:
00057                 return sprintf(__n('%d Byte', '%d Bytes', $size, true), $size);
00058             case round($size / 1024) < 1024:
00059                 return sprintf(__('%d KB', true), $this->precision($size / 1024, 0));
00060             case round($size / 1024 / 1024, 2) < 1024:
00061                 return sprintf(__('%.2f MB', true), $this->precision($size / 1024 / 1024, 2));
00062             case round($size / 1024 / 1024 / 1024, 2) < 1024:
00063                 return sprintf(__('%.2f GB', true), $this->precision($size / 1024 / 1024 / 1024, 2));
00064             default:
00065                 return sprintf(__('%.2f TB', true), $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
00066         }
00067     }
00068 /**
00069  * Formats a number into a percentage string.
00070  *
00071  * @param float $number A floating point number
00072  * @param integer $precision The precision of the returned number
00073  * @return string Percentage string
00074  * @static
00075  */
00076     function toPercentage($number, $precision = 2) {
00077         return $this->precision($number, $precision) . '%';
00078     }
00079 /**
00080  * Formats a number into a currency format.
00081  *
00082  * @param float $number A floating point number
00083  * @param integer $options if int then places, if string then before, if (,.-) then use it
00084  *   or array with places and before keys
00085  * @return string formatted number
00086  * @static
00087  */
00088     function format($number, $options = false) {
00089         $places = 0;
00090         if (is_int($options)) {
00091             $places = $options;
00092         }
00093 
00094         $separators = array(',', '.', '-', ':');
00095 
00096         $before = $after = null;
00097         if (is_string($options) && !in_array($options, $separators)) {
00098             $before = $options;
00099         }
00100         $thousands = ',';
00101         if (!is_array($options) && in_array($options, $separators)) {
00102             $thousands = $options;
00103         }
00104         $decimals = '.';
00105         if (!is_array($options) && in_array($options, $separators)) {
00106             $decimals = $options;
00107         }
00108 
00109         $escape = true;
00110         if (is_array($options)) {
00111             $options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
00112             extract($options);
00113         }
00114 
00115         $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
00116 
00117         if ($escape) {
00118             return h($out);
00119         }
00120         return $out;
00121     }
00122 /**
00123  * Formats a number into a currency format.
00124  *
00125  * @param float $number
00126  * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
00127  *   set at least 'before' and 'after' options.
00128  * @param array $options
00129  * @return string Number formatted as a currency.
00130  */
00131     function currency($number, $currency = 'USD', $options = array()) {
00132         $default = array(
00133             'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
00134             'decimals' => '.','negative' => '()', 'escape' => true
00135         );
00136         $currencies = array(
00137             'USD' => array(
00138                 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
00139                 'decimals' => '.', 'negative' => '()', 'escape' => true
00140             ),
00141             'GBP' => array(
00142                 'before'=>'&#163;', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
00143                 'decimals' => '.', 'negative' => '()','escape' => false
00144             ),
00145             'EUR' => array(
00146                 'before'=>'&#8364;', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
00147                 'decimals' => ',', 'negative' => '()', 'escape' => false
00148             )
00149         );
00150 
00151         if (isset($currencies[$currency])) {
00152             $default = $currencies[$currency];
00153         } elseif (is_string($currency)) {
00154             $options['before'] = $currency;
00155         }
00156 
00157         $options = array_merge($default, $options);
00158 
00159         $result = null;
00160 
00161         if ($number == 0 ) {
00162             if ($options['zero'] !== 0 ) {
00163                 return $options['zero'];
00164             }
00165             $options['after'] = null;
00166         } elseif ($number < 1 && $number > -1 ) {
00167             $multiply = intval('1' . str_pad('', $options['places'], '0'));
00168             $number = $number * $multiply;
00169             $options['before'] = null;
00170             $options['places'] = null;
00171         } elseif (empty($options['before'])) {
00172             $options['before'] = null;
00173         } else {
00174             $options['after'] = null;
00175         }
00176 
00177         $abs = abs($number);
00178         $result = $this->format($abs, $options);
00179 
00180         if ($number < 0 ) {
00181             if ($options['negative'] == '()') {
00182                 $result = '(' . $result .')';
00183             } else {
00184                 $result = $options['negative'] . $result;
00185             }
00186         }
00187         return $result;
00188     }
00189 }
00190 ?>

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7