CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  3: /**
  4:  * Number Helper.
  5:  *
  6:  * Methods to make numbers more readable.
  7:  *
  8:  * PHP versions 4 and 5
  9:  *
 10:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 11:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 12:  *
 13:  * Licensed under The MIT License
 14:  * Redistributions of files must retain the above copyright notice.
 15:  *
 16:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 17:  * @link          http://cakephp.org CakePHP(tm) Project
 18:  * @package       cake
 19:  * @subpackage    cake.cake.libs.view.helpers
 20:  * @since         CakePHP(tm) v 0.10.0.1076
 21:  * @version       $Revision$
 22:  * @modifiedby    $LastChangedBy$
 23:  * @lastmodified  $Date$
 24:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 25:  */
 26: /**
 27:  * Number helper library.
 28:  *
 29:  * Methods to make numbers more readable.
 30:  *
 31:  * @package       cake
 32:  * @subpackage    cake.cake.libs.view.helpers
 33:  */
 34: class NumberHelper extends AppHelper {
 35: /**
 36:  * Formats a number with a level of precision.
 37:  *
 38:  * @param  float    $number A floating point number.
 39:  * @param  integer $precision The precision of the returned number.
 40:  * @return float Enter description here...
 41:  * @static
 42:  */
 43:     function precision($number, $precision = 3) {
 44:         return sprintf("%01.{$precision}f", $number);
 45:     }
 46: /**
 47:  * Returns a formatted-for-humans file size.
 48:  *
 49:  * @param integer $length Size in bytes
 50:  * @return string Human readable size
 51:  * @static
 52:  */
 53:     function toReadableSize($size) {
 54:         switch (true) {
 55:             case $size < 1024:
 56:                 return sprintf(__n('%d Byte', '%d Bytes', $size, true), $size);
 57:             case round($size / 1024) < 1024:
 58:                 return sprintf(__('%d KB', true), $this->precision($size / 1024, 0));
 59:             case round($size / 1024 / 1024, 2) < 1024:
 60:                 return sprintf(__('%.2f MB', true), $this->precision($size / 1024 / 1024, 2));
 61:             case round($size / 1024 / 1024 / 1024, 2) < 1024:
 62:                 return sprintf(__('%.2f GB', true), $this->precision($size / 1024 / 1024 / 1024, 2));
 63:             default:
 64:                 return sprintf(__('%.2f TB', true), $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
 65:         }
 66:     }
 67: /**
 68:  * Formats a number into a percentage string.
 69:  *
 70:  * @param float $number A floating point number
 71:  * @param integer $precision The precision of the returned number
 72:  * @return string Percentage string
 73:  * @static
 74:  */
 75:     function toPercentage($number, $precision = 2) {
 76:         return $this->precision($number, $precision) . '%';
 77:     }
 78: /**
 79:  * Formats a number into a currency format.
 80:  *
 81:  * @param float $number A floating point number
 82:  * @param integer $options if int then places, if string then before, if (,.-) then use it
 83:  *   or array with places and before keys
 84:  * @return string formatted number
 85:  * @static
 86:  */
 87:     function format($number, $options = false) {
 88:         $places = 0;
 89:         if (is_int($options)) {
 90:             $places = $options;
 91:         }
 92: 
 93:         $separators = array(',', '.', '-', ':');
 94: 
 95:         $before = $after = null;
 96:         if (is_string($options) && !in_array($options, $separators)) {
 97:             $before = $options;
 98:         }
 99:         $thousands = ',';
100:         if (!is_array($options) && in_array($options, $separators)) {
101:             $thousands = $options;
102:         }
103:         $decimals = '.';
104:         if (!is_array($options) && in_array($options, $separators)) {
105:             $decimals = $options;
106:         }
107: 
108:         $escape = true;
109:         if (is_array($options)) {
110:             $options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
111:             extract($options);
112:         }
113: 
114:         $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
115: 
116:         if ($escape) {
117:             return h($out);
118:         }
119:         return $out;
120:     }
121: /**
122:  * Formats a number into a currency format.
123:  *
124:  * @param float $number
125:  * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
126:  *   set at least 'before' and 'after' options.
127:  * @param array $options
128:  * @return string Number formatted as a currency.
129:  */
130:     function currency($number, $currency = 'USD', $options = array()) {
131:         $default = array(
132:             'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
133:             'decimals' => '.','negative' => '()', 'escape' => true
134:         );
135:         $currencies = array(
136:             'USD' => array(
137:                 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
138:                 'decimals' => '.', 'negative' => '()', 'escape' => true
139:             ),
140:             'GBP' => array(
141:                 'before'=>'&#163;', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
142:                 'decimals' => '.', 'negative' => '()','escape' => false
143:             ),
144:             'EUR' => array(
145:                 'before'=>'&#8364;', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
146:                 'decimals' => ',', 'negative' => '()', 'escape' => false
147:             )
148:         );
149: 
150:         if (isset($currencies[$currency])) {
151:             $default = $currencies[$currency];
152:         } elseif (is_string($currency)) {
153:             $options['before'] = $currency;
154:         }
155: 
156:         $options = array_merge($default, $options);
157: 
158:         $result = null;
159: 
160:         if ($number == 0 ) {
161:             if ($options['zero'] !== 0 ) {
162:                 return $options['zero'];
163:             }
164:             $options['after'] = null;
165:         } elseif ($number < 1 && $number > -1 ) {
166:             $multiply = intval('1' . str_pad('', $options['places'], '0'));
167:             $number = $number * $multiply;
168:             $options['before'] = null;
169:             $options['places'] = null;
170:         } elseif (empty($options['before'])) {
171:             $options['before'] = null;
172:         } else {
173:             $options['after'] = null;
174:         }
175: 
176:         $abs = abs($number);
177:         $result = $this->format($abs, $options);
178: 
179:         if ($number < 0 ) {
180:             if ($options['negative'] == '()') {
181:                 $result = '(' . $result .')';
182:             } else {
183:                 $result = $options['negative'] . $result;
184:             }
185:         }
186:         return $result;
187:     }
188: }
189: ?>
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs