2012-02-07 01:03:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakeNumber Utility.
|
|
|
|
*
|
|
|
|
* Methods to make numbers more readable.
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-02-07 01:03:21 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2012-02-07 01:03:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-02-07 01:03:21 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package Cake.Utility
|
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2012-02-07 01:03:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number helper library.
|
|
|
|
*
|
|
|
|
* Methods to make numbers more readable.
|
|
|
|
*
|
|
|
|
* @package Cake.Utility
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html
|
|
|
|
*/
|
|
|
|
class CakeNumber {
|
|
|
|
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Currencies supported by the helper. You can add additional currency formats
|
2012-02-07 01:03:21 +00:00
|
|
|
* with CakeNumber::addFormat
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-02-05 22:56:56 +00:00
|
|
|
protected static $_currencies = array(
|
2013-03-06 22:35:18 +00:00
|
|
|
'AUD' => array(
|
|
|
|
'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
|
2013-08-06 22:36:37 +00:00
|
|
|
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
|
|
|
|
'fractionExponent' => 2
|
2013-03-06 22:35:18 +00:00
|
|
|
),
|
|
|
|
'CAD' => array(
|
|
|
|
'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
|
2013-08-06 22:36:37 +00:00
|
|
|
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
|
|
|
|
'fractionExponent' => 2
|
2013-03-06 22:35:18 +00:00
|
|
|
),
|
2012-02-07 01:03:21 +00:00
|
|
|
'USD' => array(
|
|
|
|
'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
|
2013-08-06 22:36:37 +00:00
|
|
|
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
|
|
|
|
'fractionExponent' => 2
|
2012-02-07 01:03:21 +00:00
|
|
|
),
|
2013-03-06 22:35:18 +00:00
|
|
|
'EUR' => array(
|
|
|
|
'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
|
2013-08-06 22:36:37 +00:00
|
|
|
'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => true,
|
|
|
|
'fractionExponent' => 0
|
2013-03-06 22:35:18 +00:00
|
|
|
),
|
2012-02-07 01:03:21 +00:00
|
|
|
'GBP' => array(
|
2013-03-06 22:35:18 +00:00
|
|
|
'wholeSymbol' => '£', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
|
2013-08-12 14:00:24 +00:00
|
|
|
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
|
2013-08-06 22:36:37 +00:00
|
|
|
'fractionExponent' => 2
|
2013-03-06 22:35:18 +00:00
|
|
|
),
|
|
|
|
'JPY' => array(
|
2013-08-06 22:36:37 +00:00
|
|
|
'wholeSymbol' => '¥', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
|
|
|
|
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
|
|
|
|
'fractionExponent' => 0
|
2012-02-07 01:03:21 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default options for currency formats
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-02-05 22:56:56 +00:00
|
|
|
protected static $_currencyDefaults = array(
|
2012-02-07 01:03:21 +00:00
|
|
|
'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => '', 'fractionPosition' => 'after',
|
2013-08-07 23:03:21 +00:00
|
|
|
'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
|
2013-08-06 22:36:37 +00:00
|
|
|
'fractionExponent' => 2
|
2012-02-07 01:03:21 +00:00
|
|
|
);
|
|
|
|
|
2012-10-26 17:32:39 +00:00
|
|
|
/**
|
|
|
|
* Default currency used by CakeNumber::currency()
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_defaultCurrency = 'USD';
|
|
|
|
|
2012-03-28 06:43:55 +00:00
|
|
|
/**
|
|
|
|
* If native number_format() should be used. If >= PHP5.4
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected static $_numberFormatSupport = null;
|
|
|
|
|
2012-02-07 01:03:21 +00:00
|
|
|
/**
|
|
|
|
* Formats a number with a level of precision.
|
|
|
|
*
|
2012-09-19 23:41:41 +00:00
|
|
|
* @param float $value A floating point number.
|
2012-02-07 01:03:21 +00:00
|
|
|
* @param integer $precision The precision of the returned number.
|
|
|
|
* @return float Formatted float.
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
|
|
|
|
*/
|
2012-09-19 23:41:41 +00:00
|
|
|
public static function precision($value, $precision = 3) {
|
2013-08-14 14:00:10 +00:00
|
|
|
return sprintf("%01.{$precision}f", $value);
|
2012-02-07 01:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a formatted-for-humans file size.
|
|
|
|
*
|
|
|
|
* @param integer $size Size in bytes
|
|
|
|
* @return string Human readable size
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
|
|
|
|
*/
|
2012-02-13 02:13:39 +00:00
|
|
|
public static function toReadableSize($size) {
|
2012-02-07 01:03:21 +00:00
|
|
|
switch (true) {
|
|
|
|
case $size < 1024:
|
|
|
|
return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
|
|
|
|
case round($size / 1024) < 1024:
|
2013-08-14 14:00:10 +00:00
|
|
|
return __d('cake', '%s KB', self::precision($size / 1024, 0));
|
2012-02-07 01:03:21 +00:00
|
|
|
case round($size / 1024 / 1024, 2) < 1024:
|
2013-08-14 14:00:10 +00:00
|
|
|
return __d('cake', '%s MB', self::precision($size / 1024 / 1024, 2));
|
2012-02-07 01:03:21 +00:00
|
|
|
case round($size / 1024 / 1024 / 1024, 2) < 1024:
|
2013-08-14 14:00:10 +00:00
|
|
|
return __d('cake', '%s GB', self::precision($size / 1024 / 1024 / 1024, 2));
|
2012-02-07 01:03:21 +00:00
|
|
|
default:
|
2013-08-14 14:00:10 +00:00
|
|
|
return __d('cake', '%s TB', self::precision($size / 1024 / 1024 / 1024 / 1024, 2));
|
2012-02-07 01:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 15:34:30 +00:00
|
|
|
/**
|
|
|
|
* Converts filesize from human readable string to bytes
|
|
|
|
*
|
2012-12-21 19:39:02 +00:00
|
|
|
* @param string $size Size in human readable string like '5MB', '5M', '500B', '50kb' etc.
|
2012-09-17 09:29:13 +00:00
|
|
|
* @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type'
|
2012-12-21 19:39:02 +00:00
|
|
|
* @return mixed Number of bytes as integer on success, `$default` on failure if not false
|
2012-09-18 18:52:46 +00:00
|
|
|
* @throws CakeException On invalid Unit type.
|
2013-09-01 00:32:48 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::fromReadableSize
|
2012-06-25 15:34:30 +00:00
|
|
|
*/
|
2012-09-17 09:29:13 +00:00
|
|
|
public static function fromReadableSize($size, $default = false) {
|
2012-06-25 15:34:30 +00:00
|
|
|
if (ctype_digit($size)) {
|
2012-12-21 19:39:02 +00:00
|
|
|
return (int)$size;
|
2012-06-25 15:34:30 +00:00
|
|
|
}
|
2012-09-18 15:15:33 +00:00
|
|
|
$size = strtoupper($size);
|
2012-09-18 07:34:05 +00:00
|
|
|
|
2012-12-21 19:39:02 +00:00
|
|
|
$l = -2;
|
2012-09-18 07:34:05 +00:00
|
|
|
$i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB'));
|
2012-12-21 19:39:02 +00:00
|
|
|
if ($i === false) {
|
|
|
|
$l = -1;
|
|
|
|
$i = array_search(substr($size, -1), array('K', 'M', 'G', 'T', 'P'));
|
|
|
|
}
|
2012-09-18 07:34:05 +00:00
|
|
|
if ($i !== false) {
|
2012-12-21 19:39:02 +00:00
|
|
|
$size = substr($size, 0, $l);
|
2012-09-18 07:34:05 +00:00
|
|
|
return $size * pow(1024, $i + 1);
|
2012-06-25 15:34:30 +00:00
|
|
|
}
|
2012-09-18 07:34:05 +00:00
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if (substr($size, -1) === 'B' && ctype_digit(substr($size, 0, -1))) {
|
2012-12-21 19:39:02 +00:00
|
|
|
$size = substr($size, 0, -1);
|
|
|
|
return (int)$size;
|
2012-06-25 15:34:30 +00:00
|
|
|
}
|
2012-09-18 07:34:05 +00:00
|
|
|
|
2012-09-17 09:29:13 +00:00
|
|
|
if ($default !== false) {
|
|
|
|
return $default;
|
|
|
|
}
|
2012-06-25 15:34:30 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'No unit type.'));
|
|
|
|
}
|
|
|
|
|
2012-02-07 01:03:21 +00:00
|
|
|
/**
|
|
|
|
* Formats a number into a percentage string.
|
|
|
|
*
|
2013-07-09 19:01:55 +00:00
|
|
|
* Options:
|
|
|
|
*
|
|
|
|
* - `multiply`: Multiply the input value by 100 for decimal percentages.
|
|
|
|
*
|
2012-09-19 23:41:41 +00:00
|
|
|
* @param float $value A floating point number
|
2012-02-07 01:03:21 +00:00
|
|
|
* @param integer $precision The precision of the returned number
|
2013-07-09 19:01:55 +00:00
|
|
|
* @param array $options Options
|
2012-02-07 01:03:21 +00:00
|
|
|
* @return string Percentage string
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
|
|
|
|
*/
|
2013-07-09 19:01:55 +00:00
|
|
|
public static function toPercentage($value, $precision = 2, $options = array()) {
|
|
|
|
$options += array('multiply' => false);
|
|
|
|
if ($options['multiply']) {
|
|
|
|
$value *= 100;
|
|
|
|
}
|
2012-09-19 23:41:41 +00:00
|
|
|
return self::precision($value, $precision) . '%';
|
2012-02-07 01:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a number into a currency format.
|
|
|
|
*
|
2012-09-19 23:41:41 +00:00
|
|
|
* @param float $value A floating point number
|
2012-02-07 01:03:21 +00:00
|
|
|
* @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
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
|
|
|
|
*/
|
2012-09-19 23:41:41 +00:00
|
|
|
public static function format($value, $options = false) {
|
2012-02-07 01:03:21 +00:00
|
|
|
$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)) {
|
2013-03-06 22:35:18 +00:00
|
|
|
$defaults = array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.');
|
|
|
|
$options += $defaults;
|
2012-02-07 01:03:21 +00:00
|
|
|
extract($options);
|
|
|
|
}
|
|
|
|
|
2012-09-19 23:41:41 +00:00
|
|
|
$value = self::_numberFormat($value, $places, '.', '');
|
|
|
|
$out = $before . self::_numberFormat($value, $places, $decimals, $thousands) . $after;
|
2012-02-07 01:03:21 +00:00
|
|
|
|
|
|
|
if ($escape) {
|
|
|
|
return h($out);
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2012-09-19 23:41:41 +00:00
|
|
|
/**
|
|
|
|
* Formats a number into a currency format to show deltas (signed differences in value).
|
|
|
|
*
|
2012-09-19 23:45:05 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
2012-09-19 23:41:41 +00:00
|
|
|
* - `places` - Number of decimal places to use. ie. 2
|
2013-08-06 22:36:37 +00:00
|
|
|
* - `fractionExponent` - Fraction exponent of this specific currency. Defaults to 2.
|
2012-09-19 23:41:41 +00:00
|
|
|
* - `before` - The string to place before whole numbers. ie. '['
|
|
|
|
* - `after` - The string to place after decimal numbers. ie. ']'
|
|
|
|
* - `thousands` - Thousands separator ie. ','
|
|
|
|
* - `decimals` - Decimal separator symbol ie. '.'
|
|
|
|
*
|
|
|
|
* @param float $value A floating point number
|
|
|
|
* @param array $options
|
|
|
|
* @return string formatted delta
|
2013-09-01 00:32:48 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::formatDelta
|
2012-09-19 23:41:41 +00:00
|
|
|
*/
|
|
|
|
public static function formatDelta($value, $options = array()) {
|
|
|
|
$places = isset($options['places']) ? $options['places'] : 0;
|
|
|
|
$value = self::_numberFormat($value, $places, '.', '');
|
|
|
|
$sign = $value > 0 ? '+' : '';
|
|
|
|
$options['before'] = isset($options['before']) ? $options['before'] . $sign : $sign;
|
|
|
|
return self::format($value, $options);
|
|
|
|
}
|
|
|
|
|
2012-03-28 06:43:55 +00:00
|
|
|
/**
|
|
|
|
* Alternative number_format() to accommodate multibyte decimals and thousands < PHP 5.4
|
|
|
|
*
|
2012-09-19 23:41:41 +00:00
|
|
|
* @param float $value
|
2012-03-28 06:43:55 +00:00
|
|
|
* @param integer $places
|
|
|
|
* @param string $decimals
|
|
|
|
* @param string $thousands
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 23:41:41 +00:00
|
|
|
protected static function _numberFormat($value, $places = 0, $decimals = '.', $thousands = ',') {
|
2012-03-28 06:43:55 +00:00
|
|
|
if (!isset(self::$_numberFormatSupport)) {
|
|
|
|
self::$_numberFormatSupport = version_compare(PHP_VERSION, '5.4.0', '>=');
|
|
|
|
}
|
|
|
|
if (self::$_numberFormatSupport) {
|
2012-09-19 23:41:41 +00:00
|
|
|
return number_format($value, $places, $decimals, $thousands);
|
2012-03-28 06:43:55 +00:00
|
|
|
}
|
2012-09-19 23:41:41 +00:00
|
|
|
$value = number_format($value, $places, '.', '');
|
2012-03-28 06:43:55 +00:00
|
|
|
$after = '';
|
2012-09-19 23:41:41 +00:00
|
|
|
$foundDecimal = strpos($value, '.');
|
2012-03-28 06:43:55 +00:00
|
|
|
if ($foundDecimal !== false) {
|
2012-09-19 23:41:41 +00:00
|
|
|
$after = substr($value, $foundDecimal);
|
|
|
|
$value = substr($value, 0, $foundDecimal);
|
2012-03-28 06:43:55 +00:00
|
|
|
}
|
2013-03-06 22:35:18 +00:00
|
|
|
while (($foundThousand = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $value)) !== $value) {
|
2012-09-19 23:41:41 +00:00
|
|
|
$value = $foundThousand;
|
2012-03-28 06:43:55 +00:00
|
|
|
}
|
2012-09-19 23:41:41 +00:00
|
|
|
$value .= $after;
|
|
|
|
return strtr($value, array(' ' => $thousands, '.' => $decimals));
|
2012-03-28 06:43:55 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 01:03:21 +00:00
|
|
|
/**
|
|
|
|
* Formats a number into a currency format.
|
|
|
|
*
|
|
|
|
* ### Options
|
|
|
|
*
|
2012-04-08 16:34:28 +00:00
|
|
|
* - `wholeSymbol` - The currency symbol to use for whole numbers,
|
|
|
|
* greater than 1, or less than -1.
|
|
|
|
* - `wholePosition` - The position the whole symbol should be placed
|
|
|
|
* valid options are 'before' & 'after'.
|
|
|
|
* - `fractionSymbol` - The currency symbol to use for fractional numbers.
|
|
|
|
* - `fractionPosition` - The position the fraction symbol should be placed
|
|
|
|
* valid options are 'before' & 'after'.
|
|
|
|
* - `before` - The currency symbol to place before whole numbers
|
|
|
|
* ie. '$'. `before` is an alias for `wholeSymbol`.
|
|
|
|
* - `after` - The currency symbol to place after decimal numbers
|
|
|
|
* ie. 'c'. Set to boolean false to use no decimal symbol.
|
2012-12-22 22:48:15 +00:00
|
|
|
* eg. 0.35 => $0.35. `after` is an alias for `fractionSymbol`
|
2012-04-08 16:34:28 +00:00
|
|
|
* - `zero` - The text to use for zero values, can be a
|
|
|
|
* string or a number. ie. 0, 'Free!'
|
2012-02-07 01:03:21 +00:00
|
|
|
* - `places` - Number of decimal places to use. ie. 2
|
2013-08-06 22:36:37 +00:00
|
|
|
* - `fractionExponent` - Fraction exponent of this specific currency. Defaults to 2.
|
2012-02-07 01:03:21 +00:00
|
|
|
* - `thousands` - Thousands separator ie. ','
|
|
|
|
* - `decimals` - Decimal separator symbol ie. '.'
|
2012-04-08 16:34:28 +00:00
|
|
|
* - `negative` - Symbol for negative numbers. If equal to '()',
|
|
|
|
* the number will be wrapped with ( and )
|
2012-11-09 03:19:51 +00:00
|
|
|
* - `escape` - Should the output be escaped for html special characters.
|
|
|
|
* The default value for this option is controlled by the currency settings.
|
2013-03-06 22:35:18 +00:00
|
|
|
* By default all currencies contain utf-8 symbols and don't need this changed. If you require
|
|
|
|
* non HTML encoded symbols you will need to update the settings with the correct bytes.
|
2012-02-07 01:03:21 +00:00
|
|
|
*
|
2012-09-19 23:41:41 +00:00
|
|
|
* @param float $value
|
2012-04-08 16:34:28 +00:00
|
|
|
* @param string $currency Shortcut to default options. Valid values are
|
|
|
|
* 'USD', 'EUR', 'GBP', otherwise set at least 'before' and 'after' options.
|
2012-02-07 01:03:21 +00:00
|
|
|
* @param array $options
|
|
|
|
* @return string Number formatted as a currency.
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
|
|
|
|
*/
|
2012-10-26 17:32:39 +00:00
|
|
|
public static function currency($value, $currency = null, $options = array()) {
|
2012-02-05 22:56:56 +00:00
|
|
|
$default = self::$_currencyDefaults;
|
2012-10-26 17:32:39 +00:00
|
|
|
if ($currency === null) {
|
|
|
|
$currency = self::defaultCurrency();
|
|
|
|
}
|
2012-02-07 01:03:21 +00:00
|
|
|
|
2012-02-05 22:56:56 +00:00
|
|
|
if (isset(self::$_currencies[$currency])) {
|
|
|
|
$default = self::$_currencies[$currency];
|
2012-02-07 01:03:21 +00:00
|
|
|
} 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';
|
2013-01-11 17:00:06 +00:00
|
|
|
$value = (float)$value;
|
2012-10-01 01:08:00 +00:00
|
|
|
if (!$value) {
|
2013-08-07 23:03:21 +00:00
|
|
|
if ($options['zero'] !== 0) {
|
2012-02-07 01:03:21 +00:00
|
|
|
return $options['zero'];
|
|
|
|
}
|
2012-09-19 23:41:41 +00:00
|
|
|
} elseif ($value < 1 && $value > -1) {
|
2012-02-07 01:03:21 +00:00
|
|
|
if ($options['fractionSymbol'] !== false) {
|
2013-08-06 22:36:37 +00:00
|
|
|
$multiply = pow(10, $options['fractionExponent']);
|
2012-09-19 23:41:41 +00:00
|
|
|
$value = $value * $multiply;
|
2012-02-07 01:03:21 +00:00
|
|
|
$options['places'] = null;
|
|
|
|
$symbolKey = 'fraction';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
$position = $options[$symbolKey . 'Position'] !== 'after' ? 'before' : 'after';
|
2012-03-03 22:31:47 +00:00
|
|
|
$options[$position] = $options[$symbolKey . 'Symbol'];
|
2012-02-07 01:03:21 +00:00
|
|
|
|
2012-09-19 23:41:41 +00:00
|
|
|
$abs = abs($value);
|
2012-02-13 02:13:39 +00:00
|
|
|
$result = self::format($abs, $options);
|
2012-02-07 01:03:21 +00:00
|
|
|
|
2012-09-19 23:41:41 +00:00
|
|
|
if ($value < 0) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($options['negative'] === '()') {
|
2012-03-03 22:31:47 +00:00
|
|
|
$result = '(' . $result . ')';
|
2012-02-07 01:03:21 +00:00
|
|
|
} else {
|
|
|
|
$result = $options['negative'] . $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Add a currency format to the Number helper. Makes reusing
|
2012-02-07 01:03:21 +00:00
|
|
|
* 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'); }}}
|
|
|
|
*
|
2012-04-08 16:34:28 +00:00
|
|
|
* Added formats are merged with the defaults defined in CakeNumber::$_currencyDefaults
|
|
|
|
* See CakeNumber::currency() for more information on the various options and their function.
|
2012-02-07 01:03:21 +00:00
|
|
|
*
|
|
|
|
* @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()
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
|
|
|
|
*/
|
2012-02-13 02:13:39 +00:00
|
|
|
public static function addFormat($formatName, $options) {
|
2012-02-05 22:56:56 +00:00
|
|
|
self::$_currencies[$formatName] = $options + self::$_currencyDefaults;
|
2012-02-07 01:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 17:32:39 +00:00
|
|
|
/**
|
|
|
|
* Getter/setter for default currency
|
|
|
|
*
|
2013-07-05 12:15:18 +00:00
|
|
|
* @param string $currency Default currency string used by currency() if $currency argument is not provided
|
2012-10-26 17:32:39 +00:00
|
|
|
* @return string Currency
|
2013-09-01 00:32:48 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::defaultCurrency
|
2012-10-26 17:32:39 +00:00
|
|
|
*/
|
|
|
|
public static function defaultCurrency($currency = null) {
|
|
|
|
if ($currency) {
|
|
|
|
self::$_defaultCurrency = $currency;
|
|
|
|
}
|
|
|
|
return self::$_defaultCurrency;
|
|
|
|
}
|
|
|
|
|
2012-02-07 01:03:21 +00:00
|
|
|
}
|