number.php

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