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 2.0 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AppHelper
  • CacheHelper
  • FormHelper
  • HtmlHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • MootoolsEngineHelper
  • NumberHelper
  • PaginatorHelper
  • PrototypeEngineHelper
  • RssHelper
  • SessionHelper
  • TextHelper
  • TimeHelper
  1: <?php
  2: /**
  3:  * Number Helper.
  4:  *
  5:  * Methods to make numbers more readable.
  6:  *
  7:  * PHP 5
  8:  *
  9:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 10:  * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * Redistributions of files must retain the above copyright notice.
 14:  *
 15:  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://cakephp.org CakePHP(tm) Project
 17:  * @package       Cake.View.Helper
 18:  * @since         CakePHP(tm) v 0.10.0.1076
 19:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 20:  */
 21: 
 22: App::uses('AppHelper', 'View/Helper');
 23: 
 24: /**
 25:  * Number helper library.
 26:  *
 27:  * Methods to make numbers more readable.
 28:  *
 29:  * @package       Cake.View.Helper
 30:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
 31:  */
 32: class NumberHelper extends AppHelper {
 33: 
 34: /**
 35:  * Currencies supported by the helper.  You can add additional currency formats
 36:  * with NumberHelper::addFormat
 37:  *
 38:  * @var array
 39:  */
 40:     protected $_currencies = array(
 41:         'USD' => array(
 42:             'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
 43:             'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
 44:         ),
 45:         'GBP' => array(
 46:             'wholeSymbol' => '&#163;', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
 47:             'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => false
 48:         ),
 49:         'EUR' => array(
 50:             'wholeSymbol' => '&#8364;', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
 51:             'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false
 52:         )
 53:     );
 54: 
 55: /**
 56:  * Default options for currency formats
 57:  *
 58:  * @var array
 59:  */
 60:     protected $_currencyDefaults = array(
 61:         'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => '', 'fractionPosition' => 'after',
 62:         'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true,
 63:     );
 64: 
 65: /**
 66:  * Formats a number with a level of precision.
 67:  *
 68:  * @param float $number A floating point number.
 69:  * @param integer $precision The precision of the returned number.
 70:  * @return float Formatted float.
 71:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
 72:  */
 73:     public function precision($number, $precision = 3) {
 74:         return sprintf("%01.{$precision}f", $number);
 75:     }
 76: 
 77: /**
 78:  * Returns a formatted-for-humans file size.
 79:  *
 80:  * @param integer $size Size in bytes
 81:  * @return string Human readable size
 82:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
 83:  */
 84:     public function toReadableSize($size) {
 85:         switch (true) {
 86:             case $size < 1024:
 87:                 return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
 88:             case round($size / 1024) < 1024:
 89:                 return __d('cake', '%d KB', $this->precision($size / 1024, 0));
 90:             case round($size / 1024 / 1024, 2) < 1024:
 91:                 return __d('cake', '%.2f MB', $this->precision($size / 1024 / 1024, 2));
 92:             case round($size / 1024 / 1024 / 1024, 2) < 1024:
 93:                 return __d('cake', '%.2f GB', $this->precision($size / 1024 / 1024 / 1024, 2));
 94:             default:
 95:                 return __d('cake', '%.2f TB', $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
 96:         }
 97:     }
 98: 
 99: /**
100:  * Formats a number into a percentage string.
101:  *
102:  * @param float $number A floating point number
103:  * @param integer $precision The precision of the returned number
104:  * @return string Percentage string
105:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
106:  */
107:     public function toPercentage($number, $precision = 2) {
108:         return $this->precision($number, $precision) . '%';
109:     }
110: 
111: /**
112:  * Formats a number into a currency format.
113:  *
114:  * @param float $number A floating point number
115:  * @param integer $options if int then places, if string then before, if (,.-) then use it
116:  *   or array with places and before keys
117:  * @return string formatted number
118:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
119:  */
120:     public function format($number, $options = false) {
121:         $places = 0;
122:         if (is_int($options)) {
123:             $places = $options;
124:         }
125: 
126:         $separators = array(',', '.', '-', ':');
127: 
128:         $before = $after = null;
129:         if (is_string($options) && !in_array($options, $separators)) {
130:             $before = $options;
131:         }
132:         $thousands = ',';
133:         if (!is_array($options) && in_array($options, $separators)) {
134:             $thousands = $options;
135:         }
136:         $decimals = '.';
137:         if (!is_array($options) && in_array($options, $separators)) {
138:             $decimals = $options;
139:         }
140: 
141:         $escape = true;
142:         if (is_array($options)) {
143:             $options = array_merge(array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
144:             extract($options);
145:         }
146: 
147:         $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
148: 
149:         if ($escape) {
150:             return h($out);
151:         }
152:         return $out;
153:     }
154: 
155: /**
156:  * Formats a number into a currency format.
157:  *
158:  * ### Options
159:  *
160:  * - `before` - The currency symbol to place before whole numbers ie. '$'
161:  * - `after` - The currency symbol to place after decimal numbers ie. 'c'. Set to boolean false to
162:  *    use no decimal symbol.  eg. 0.35 => $0.35.
163:  * - `zero` - The text to use for zero values, can be a string or a number. ie. 0, 'Free!'
164:  * - `places` - Number of decimal places to use. ie. 2
165:  * - `thousands` - Thousands separator ie. ','
166:  * - `decimals` - Decimal separator symbol ie. '.'
167:  * - `negative` - Symbol for negative numbers. If equal to '()', the number will be wrapped with ( and )
168:  * - `escape` - Should the output be htmlentity escaped? Defaults to true
169:  *
170:  * @param float $number
171:  * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
172:  *   set at least 'before' and 'after' options.
173:  * @param array $options
174:  * @return string Number formatted as a currency.
175:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
176:  */
177:     public function currency($number, $currency = 'USD', $options = array()) {
178:         $default = $this->_currencyDefaults;
179: 
180:         if (isset($this->_currencies[$currency])) {
181:             $default = $this->_currencies[$currency];
182:         } elseif (is_string($currency)) {
183:             $options['before'] = $currency;
184:         }
185: 
186:         $options = array_merge($default, $options);
187: 
188:         if (isset($options['before']) && $options['before'] !== '') {
189:             $options['wholeSymbol'] = $options['before'];
190:         }
191:         if (isset($options['after']) && !$options['after'] !== '') {
192:             $options['fractionSymbol'] = $options['after'];
193:         }
194: 
195:         $result = $options['before'] = $options['after'] = null;
196: 
197:         $symbolKey = 'whole';
198:         if ($number == 0 ) {
199:             if ($options['zero'] !== 0 ) {
200:                 return $options['zero'];
201:             }
202:         } elseif ($number < 1 && $number > -1 ) {
203:             if ($options['fractionSymbol'] !== false) {
204:                 $multiply = intval('1' . str_pad('', $options['places'], '0'));
205:                 $number = $number * $multiply;
206:                 $options['places'] = null;
207:                 $symbolKey = 'fraction';
208:             }
209:         }
210: 
211:         $position = $options[$symbolKey.'Position'] != 'after' ? 'before' : 'after';
212:         $options[$position] = $options[$symbolKey.'Symbol'];
213: 
214:         $abs = abs($number);
215:         $result = $this->format($abs, $options);
216: 
217:         if ($number < 0 ) {
218:             if ($options['negative'] == '()') {
219:                 $result = '(' . $result .')';
220:             } else {
221:                 $result = $options['negative'] . $result;
222:             }
223:         }
224:         return $result;
225:     }
226: 
227: /**
228:  * Add a currency format to the Number helper.  Makes reusing
229:  * currency formats easier.
230:  *
231:  * {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}}
232:  *
233:  * You can now use `NOK` as a shortform when formatting currency amounts.
234:  *
235:  * {{{ $number->currency($value, 'NOK'); }}}
236:  *
237:  * Added formats are merged with the following defaults.
238:  *
239:  * {{{
240:  *  array(
241:  *      'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
242:  *      'decimals' => '.', 'negative' => '()', 'escape' => true
243:  *  )
244:  * }}}
245:  *
246:  * @param string $formatName The format name to be used in the future.
247:  * @param array $options The array of options for this format.
248:  * @return void
249:  * @see NumberHelper::currency()
250:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
251:  */
252:     public function addFormat($formatName, $options) {
253:         $this->_currencies[$formatName] = $options + $this->_currencyDefaults;
254:     }
255: 
256: }
257: 
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