cakephp2-php8/lib/Cake/View/Helper/NumberHelper.php
Mark Story 91a1cf81f3 Merge branch '1.3' into merger
Conflicts:
	app/Config/acl.ini.php
	app/config/database.php.default
	app/webroot/css.php
	app/webroot/css/cake.generic.css
	cake/basics.php
	cake/bootstrap.php
	cake/config/paths.php
	cake/console/cake.php
	cake/console/error.php
	cake/console/libs/acl.php
	cake/console/libs/bake.php
	cake/console/libs/i18n.php
	cake/console/libs/shell.php
	cake/console/libs/tasks/extract.php
	cake/console/libs/tasks/plugin.php
	cake/console/libs/tasks/project.php
	cake/console/libs/testsuite.php
	cake/console/templates/default/classes/test.ctp
	cake/console/templates/default/views/home.ctp
	cake/console/templates/default/views/view.ctp
	cake/console/templates/skel/config/database.php.default
	cake/console/templates/skel/views/elements/email/text/default.ctp
	cake/console/templates/skel/webroot/css.php
	cake/dispatcher.php
	cake/libs/cache.php
	cake/libs/cake_session.php
	cake/libs/configure.php
	cake/libs/controller/component.php
	cake/libs/controller/components/auth.php
	cake/libs/controller/components/email.php
	cake/libs/controller/components/request_handler.php
	cake/libs/controller/components/security.php
	cake/libs/controller/controller.php
	cake/libs/controller/scaffold.php
	cake/libs/error.php
	cake/libs/magic_db.php
	cake/libs/model/behaviors/acl.php
	cake/libs/model/connection_manager.php
	cake/libs/model/datasources/dbo/dbo_mysqli.php
	cake/libs/model/model_behavior.php
	cake/libs/overloadable.php
	cake/libs/overloadable_php4.php
	cake/libs/overloadable_php5.php
	cake/libs/router.php
	cake/libs/view/errors/missing_action.ctp
	cake/libs/view/errors/missing_behavior_class.ctp
	cake/libs/view/errors/missing_behavior_file.ctp
	cake/libs/view/errors/missing_component_class.ctp
	cake/libs/view/errors/missing_component_file.ctp
	cake/libs/view/errors/missing_connection.ctp
	cake/libs/view/errors/missing_controller.ctp
	cake/libs/view/errors/missing_helper_class.ctp
	cake/libs/view/errors/missing_helper_file.ctp
	cake/libs/view/errors/missing_layout.ctp
	cake/libs/view/errors/missing_model.ctp
	cake/libs/view/errors/missing_scaffolddb.ctp
	cake/libs/view/errors/missing_table.ctp
	cake/libs/view/errors/missing_view.ctp
	cake/libs/view/errors/private_action.ctp
	cake/libs/view/errors/scaffold_error.ctp
	cake/libs/view/helpers/ajax.php
	cake/libs/view/helpers/javascript.php
	cake/libs/view/helpers/js.php
	cake/libs/view/helpers/session.php
	cake/libs/view/helpers/xml.php
	cake/libs/view/media.php
	cake/libs/view/pages/home.ctp
	cake/libs/view/scaffolds/edit.ctp
	cake/libs/view/scaffolds/index.ctp
	cake/libs/view/scaffolds/view.ctp
	cake/libs/view/view.php
	cake/libs/xml.php
	cake/tests/cases/console/cake.test.php
	cake/tests/cases/console/libs/acl.test.php
	cake/tests/cases/console/libs/api.test.php
	cake/tests/cases/console/libs/bake.test.php
	cake/tests/cases/console/libs/shell.test.php
	cake/tests/cases/console/libs/tasks/controller.test.php
	cake/tests/cases/console/libs/tasks/db_config.test.php
	cake/tests/cases/console/libs/tasks/fixture.test.php
	cake/tests/cases/console/libs/tasks/model.test.php
	cake/tests/cases/console/libs/tasks/plugin.test.php
	cake/tests/cases/console/libs/tasks/project.test.php
	cake/tests/cases/console/libs/tasks/test.test.php
	cake/tests/cases/console/libs/tasks/view.test.php
	cake/tests/cases/dispatcher.test.php
	cake/tests/cases/libs/cache/apc.test.php
	cake/tests/cases/libs/cake_session.test.php
	cake/tests/cases/libs/cake_test_case.test.php
	cake/tests/cases/libs/code_coverage_manager.test.php
	cake/tests/cases/libs/configure.test.php
	cake/tests/cases/libs/controller/component.test.php
	cake/tests/cases/libs/controller/components/auth.test.php
	cake/tests/cases/libs/controller/components/cookie.test.php
	cake/tests/cases/libs/controller/components/request_handler.test.php
	cake/tests/cases/libs/controller/components/session.test.php
	cake/tests/cases/libs/controller/controller.test.php
	cake/tests/cases/libs/controller/pages_controller.test.php
	cake/tests/cases/libs/error.test.php
	cake/tests/cases/libs/http_socket.test.php
	cake/tests/cases/libs/magic_db.test.php
	cake/tests/cases/libs/model/datasources/dbo/dbo_mssql.test.php
	cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
	cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php
	cake/tests/cases/libs/model/datasources/dbo_source.test.php
	cake/tests/cases/libs/model/models.php
	cake/tests/cases/libs/overloadable.test.php
	cake/tests/cases/libs/test_manager.test.php
	cake/tests/cases/libs/view/helpers/ajax.test.php
	cake/tests/cases/libs/view/helpers/javascript.test.php
	cake/tests/cases/libs/view/helpers/session.test.php
	cake/tests/cases/libs/view/helpers/xml.test.php
	cake/tests/cases/libs/view/media.test.php
	cake/tests/cases/libs/view/theme.test.php
	cake/tests/cases/libs/xml.test.php
	cake/tests/fixtures/aco_fixture.php
	cake/tests/fixtures/translate_fixture.php
	cake/tests/groups/acl.group.php
	cake/tests/groups/bake.group.php
	cake/tests/groups/behaviors.group.php
	cake/tests/groups/cache.group.php
	cake/tests/groups/components.group.php
	cake/tests/groups/configure.group.php
	cake/tests/groups/console.group.php
	cake/tests/groups/controller.group.php
	cake/tests/groups/database.group.php
	cake/tests/groups/helpers.group.php
	cake/tests/groups/i18n.group.php
	cake/tests/groups/javascript.group.php
	cake/tests/groups/lib.group.php
	cake/tests/groups/model.group.php
	cake/tests/groups/no_cross_contamination.group.php
	cake/tests/groups/routing_system.group.php
	cake/tests/groups/socket.group.php
	cake/tests/groups/test_suite.group.php
	cake/tests/groups/view.group.php
	cake/tests/groups/xml.group.php
	cake/tests/lib/cake_test_case.php
	cake/tests/lib/cake_test_model.php
	cake/tests/lib/cake_test_suite_dispatcher.php
	cake/tests/lib/cake_web_test_case.php
	cake/tests/lib/code_coverage_manager.php
	cake/tests/lib/reporter/cake_base_reporter.php
	cake/tests/lib/reporter/cake_cli_reporter.php
	cake/tests/lib/reporter/cake_text_reporter.php
	cake/tests/lib/templates/menu.php
	cake/tests/lib/templates/simpletest.php
	cake/tests/lib/test_manager.php
	cake/tests/test_app/controllers/tests_apps_controller.php
	cake/tests/test_app/libs/cache/test_app_cache.php
	cake/tests/test_app/libs/library.php
	cake/tests/test_app/libs/log/test_app_log.php
	cake/tests/test_app/plugins/test_plugin/config/load.php
	cake/tests/test_app/plugins/test_plugin/config/more.load.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/other_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/plugins_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/components/test_plugin_other_component.php
	cake/tests/test_app/plugins/test_plugin/controllers/test_plugin_controller.php
	cake/tests/test_app/plugins/test_plugin/controllers/tests_controller.php
	cake/tests/test_app/plugins/test_plugin/libs/cache/test_plugin_cache.php
	cake/tests/test_app/plugins/test_plugin/libs/log/test_plugin_log.php
	cake/tests/test_app/plugins/test_plugin/libs/test_plugin_library.php
	cake/tests/test_app/plugins/test_plugin/test_plugin_app_controller.php
	cake/tests/test_app/plugins/test_plugin/test_plugin_app_model.php
	cake/tests/test_app/plugins/test_plugin/vendors/sample/sample_plugin.php
	cake/tests/test_app/plugins/test_plugin/vendors/welcome.php
	cake/tests/test_app/plugins/test_plugin/views/helpers/other_helper.php
	cake/tests/test_app/plugins/test_plugin/views/helpers/plugged_helper.php
	cake/tests/test_app/vendors/Test/MyTest.php
	cake/tests/test_app/vendors/Test/hello.php
	cake/tests/test_app/vendors/sample/configure_test_vendor_sample.php
	cake/tests/test_app/vendors/shells/sample.php
	cake/tests/test_app/vendors/somename/some.name.php
	cake/tests/test_app/vendors/welcome.php
	cake/tests/test_app/views/elements/email/text/default.ctp
	cake/tests/test_app/views/layouts/default.ctp
	cake/tests/test_app/views/posts/test_nocache_tags.ctp
	lib/Cake/Cache/Engine/MemcacheEngine.php
	lib/Cake/Config/config.php
	lib/Cake/Console/Command/Task/ModelTask.php
	lib/Cake/Console/Templates/skel/webroot/css/cake.generic.css
	lib/Cake/Console/Templates/skel/webroot/test.php
	lib/Cake/Console/cake.bat
	lib/Cake/Controller/Component/CookieComponent.php
	lib/Cake/Log/CakeLog.php
	lib/Cake/Model/CakeSchema.php
	lib/Cake/Test/Case/Log/Engine/FileLog.php
	lib/Cake/Test/Case/View/Helper/FormHelperTest.php
	lib/Cake/Test/test_app/View/Emails/html/custom.ctp
	lib/Cake/Test/test_app/View/Emails/text/custom.ctp
	lib/Cake/TestSuite/templates/header.php
	lib/Cake/Utility/Sanitize.php
	lib/Cake/Utility/Validation.php
	lib/Cake/VERSION.txt
	lib/Cake/View/Helper/FormHelper.php
2011-06-23 12:48:06 -07:00

262 lines
8.1 KiB
PHP

<?php
/**
* Number Helper.
*
* Methods to make numbers more readable.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.libs.view.helpers
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('AppHelper', 'View/Helper');
/**
* Number helper library.
*
* Methods to make numbers more readable.
*
* @package cake.libs.view.helpers
* @link http://book.cakephp.org/view/1452/Number
*/
class NumberHelper extends AppHelper {
/**
* Currencies supported by the helper. You can add additional currency formats
* with NumberHelper::addFormat
*
* @var array
* @access protected
*/
protected $_currencies = array(
'USD' => array(
'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
),
'GBP' => array(
'wholeSymbol'=>'&#163;', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => false
),
'EUR' => array(
'wholeSymbol'=>'&#8364;', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false
)
);
/**
* Default options for currency formats
*
* @var array
* @access protected
*/
protected $_currencyDefaults = array(
'wholeSymbol'=>'', 'wholePosition' => 'before', 'fractionSymbol' => '', 'fractionPosition' => 'after',
'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true
);
/**
* Formats a number with a level of precision.
*
* @param float $number A floating point number.
* @param integer $precision The precision of the returned number.
* @return float Formatted float.
* @access public
* @link http://book.cakephp.org/view/1454/precision
*/
public function precision($number, $precision = 3) {
return sprintf("%01.{$precision}f", $number);
}
/**
* Returns a formatted-for-humans file size.
*
* @param integer $length Size in bytes
* @return string Human readable size
* @access public
* @link http://book.cakephp.org/view/1456/toReadableSize
*/
public function toReadableSize($size) {
switch (true) {
case $size < 1024:
return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
case round($size / 1024) < 1024:
return __d('cake', '%d KB', $this->precision($size / 1024, 0));
case round($size / 1024 / 1024, 2) < 1024:
return __d('cake', '%.2f MB', $this->precision($size / 1024 / 1024, 2));
case round($size / 1024 / 1024 / 1024, 2) < 1024:
return __d('cake', '%.2f GB', $this->precision($size / 1024 / 1024 / 1024, 2));
default:
return __d('cake', '%.2f TB', $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
}
}
/**
* 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
* @access public
* @link http://book.cakephp.org/view/1455/toPercentage
*/
public function toPercentage($number, $precision = 2) {
return $this->precision($number, $precision) . '%';
}
/**
* Formats a number into a currency format.
*
* @param float $number A floating point number
* @param integer $options if int then places, if string then before, if (,.-) then use it
* or array with places and before keys
* @return string formatted number
* @access public
* @link http://book.cakephp.org/view/1457/format
*/
public function format($number, $options = false) {
$places = 0;
if (is_int($options)) {
$places = $options;
}
$separators = array(',', '.', '-', ':');
$before = $after = null;
if (is_string($options) && !in_array($options, $separators)) {
$before = $options;
}
$thousands = ',';
if (!is_array($options) && in_array($options, $separators)) {
$thousands = $options;
}
$decimals = '.';
if (!is_array($options) && in_array($options, $separators)) {
$decimals = $options;
}
$escape = true;
if (is_array($options)) {
$options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
extract($options);
}
$out = $before . number_format($number, $places, $decimals, $thousands) . $after;
if ($escape) {
return h($out);
}
return $out;
}
/**
* Formats a number into a currency format.
*
* ### Options
*
* - `before` - The currency symbol to place before whole numbers ie. '$'
* - `after` - The currency symbol to place after decimal numbers ie. 'c'. Set to boolean false to
* use no decimal symbol. eg. 0.35 => $0.35.
* - `zero` - The text to use for zero values, can be a string or a number. ie. 0, 'Free!'
* - `places` - Number of decimal places to use. ie. 2
* - `thousands` - Thousands separator ie. ','
* - `decimals` - Decimal separator symbol ie. '.'
* - `negative` - Symbol for negative numbers. If equal to '()', the number will be wrapped with ( and )
* - `escape` - Should the output be htmlentity escaped? Defaults to true
*
* @param float $number
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
* set at least 'before' and 'after' options.
* @param array $options
* @return string Number formatted as a currency.
* @access public
* @link http://book.cakephp.org/view/1453/currency
*/
public function currency($number, $currency = 'USD', $options = array()) {
$default = $this->_currencyDefaults;
if (isset($this->_currencies[$currency])) {
$default = $this->_currencies[$currency];
} elseif (is_string($currency)) {
$options['before'] = $currency;
}
$options = array_merge($default, $options);
if (isset($options['before']) && $options['before'] !== '') {
$options['wholeSymbol'] = $options['before'];
}
if (isset($options['after']) && !$options['after'] !== '') {
$options['fractionSymbol'] = $options['after'];
}
$result = $options['before'] = $options['after'] = null;
$symbolKey = 'whole';
if ($number == 0 ) {
if ($options['zero'] !== 0 ) {
return $options['zero'];
}
} elseif ($number < 1 && $number > -1 ) {
if ($options['fractionSymbol'] !== false) {
$multiply = intval('1' . str_pad('', $options['places'], '0'));
$number = $number * $multiply;
$options['places'] = null;
$symbolKey = 'fraction';
}
}
$position = $options[$symbolKey.'Position'] != 'after' ? 'before' : 'after';
$options[$position] = $options[$symbolKey.'Symbol'];
$abs = abs($number);
$result = $this->format($abs, $options);
if ($number < 0 ) {
if ($options['negative'] == '()') {
$result = '(' . $result .')';
} else {
$result = $options['negative'] . $result;
}
}
return $result;
}
/**
* Add a currency format to the Number helper. Makes reusing
* currency formats easier.
*
* {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}}
*
* You can now use `NOK` as a shortform when formatting currency amounts.
*
* {{{ $number->currency($value, 'NOK'); }}}
*
* Added formats are merged with the following defaults.
*
* {{{
* array(
* 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
* 'decimals' => '.', 'negative' => '()', 'escape' => true
* )
* }}}
*
* @param string $formatName The format name to be used in the future.
* @param array $options The array of options for this format.
* @return void
* @see NumberHelper::currency()
*/
public function addFormat($formatName, $options) {
$this->_currencies[$formatName] = $options + $this->_currencyDefaults;
}
}