Skip to content
This repository
Browse code

adding CakeNumber::formatDelta() and fixing issue with near-zero valu…

…es and format()
  • Loading branch information...
commit 3fa6b96ad0ff02f1af67b55e25af08b5156696f0 1 parent 99cbd22
Mark authored September 20, 2012
46  lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -70,6 +70,52 @@ public function testFormat() {
70 70
 		$result = $this->Number->format($value, '-');
71 71
 		$expected = '100-100-100';
72 72
 		$this->assertEquals($expected, $result);
  73
+
  74
+		$value = 0.00001;
  75
+		$result = $this->Number->format($value, array('places' => 1));
  76
+		$expected = '$0.0';
  77
+		$this->assertEquals($expected, $result);
  78
+
  79
+		$value = -0.00001;
  80
+		$result = $this->Number->format($value, array('places' => 1));
  81
+		$expected = '$0.0';
  82
+		$this->assertEquals($expected, $result);
  83
+	}
  84
+
  85
+/**
  86
+ * testFormatDelta method
  87
+ *
  88
+ * @return void
  89
+ */
  90
+	public function testFormatDelta() {
  91
+		$value = '100100100';
  92
+
  93
+		$result = $this->Number->formatDelta($value, array('before' => '', 'after' => ''));
  94
+		$expected = '+100,100,100.00';
  95
+		$this->assertEquals($expected, $result);
  96
+
  97
+		$result = $this->Number->formatDelta($value, array('before' => '[', 'after' => ']'));
  98
+		$expected = '[+100,100,100.00]';
  99
+		$this->assertEquals($expected, $result);
  100
+
  101
+		$result = $this->Number->formatDelta(-$value, array('before' => '[', 'after' => ']'));
  102
+		$expected = '[-100,100,100.00]';
  103
+		$this->assertEquals($expected, $result);
  104
+
  105
+		$value = 0;
  106
+		$result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
  107
+		$expected = '[0.0]';
  108
+		$this->assertEquals($expected, $result);
  109
+
  110
+		$value = 0.0001;
  111
+		$result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
  112
+		$expected = '[0.0]';
  113
+		$this->assertEquals($expected, $result);
  114
+
  115
+		$value = 9876.1234;
  116
+		$result = $this->Number->formatDelta($value, array('places' => 1, 'decimals' => ',', 'thousands' => '.'));
  117
+		$expected = '+9.876,1';
  118
+		$this->assertEquals($expected, $result);
73 119
 	}
74 120
 
75 121
 /**
78  lib/Cake/Utility/CakeNumber.php
@@ -70,13 +70,13 @@ class CakeNumber {
70 70
 /**
71 71
  * Formats a number with a level of precision.
72 72
  *
73  
- * @param float $number A floating point number.
  73
+ * @param float $value A floating point number.
74 74
  * @param integer $precision The precision of the returned number.
75 75
  * @return float Formatted float.
76 76
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
77 77
  */
78  
-	public static function precision($number, $precision = 3) {
79  
-		return sprintf("%01.{$precision}F", $number);
  78
+	public static function precision($value, $precision = 3) {
  79
+		return sprintf("%01.{$precision}F", $value);
80 80
 	}
81 81
 
82 82
 /**
@@ -135,25 +135,25 @@ public static function fromReadableSize($size, $default = false) {
135 135
 /**
136 136
  * Formats a number into a percentage string.
137 137
  *
138  
- * @param float $number A floating point number
  138
+ * @param float $value A floating point number
139 139
  * @param integer $precision The precision of the returned number
140 140
  * @return string Percentage string
141 141
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
142 142
  */
143  
-	public static function toPercentage($number, $precision = 2) {
144  
-		return self::precision($number, $precision) . '%';
  143
+	public static function toPercentage($value, $precision = 2) {
  144
+		return self::precision($value, $precision) . '%';
145 145
 	}
146 146
 
147 147
 /**
148 148
  * Formats a number into a currency format.
149 149
  *
150  
- * @param float $number A floating point number
  150
+ * @param float $value A floating point number
151 151
  * @param integer $options if int then places, if string then before, if (,.-) then use it
152 152
  *   or array with places and before keys
153 153
  * @return string formatted number
154 154
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
155 155
  */
156  
-	public static function format($number, $options = false) {
  156
+	public static function format($value, $options = false) {
157 157
 		$places = 0;
158 158
 		if (is_int($options)) {
159 159
 			$places = $options;
@@ -180,7 +180,8 @@ public static function format($number, $options = false) {
180 180
 			extract($options);
181 181
 		}
182 182
 
183  
-		$out = $before . self::_numberFormat($number, $places, $decimals, $thousands) . $after;
  183
+		$value = self::_numberFormat($value, $places, '.', '');
  184
+		$out = $before . self::_numberFormat($value, $places, $decimals, $thousands) . $after;
184 185
 
185 186
 		if ($escape) {
186 187
 			return h($out);
@@ -189,33 +190,54 @@ public static function format($number, $options = false) {
189 190
 	}
190 191
 
191 192
 /**
  193
+ * Formats a number into a currency format to show deltas (signed differences in value).
  194
+ *
  195
+ * - `places` - Number of decimal places to use. ie. 2
  196
+ * - `before` - The string to place before whole numbers. ie. '['
  197
+ * - `after` - The string to place after decimal numbers. ie. ']'
  198
+ * - `thousands` - Thousands separator ie. ','
  199
+ * - `decimals` - Decimal separator symbol ie. '.'
  200
+ *
  201
+ * @param float $value A floating point number
  202
+ * @param array $options
  203
+ * @return string formatted delta
  204
+ */
  205
+	public static function formatDelta($value, $options = array()) {
  206
+		$places = isset($options['places']) ? $options['places'] : 0;
  207
+		$value = self::_numberFormat($value, $places, '.', '');
  208
+		$sign = $value > 0 ? '+' : '';
  209
+		$options['before'] = isset($options['before']) ? $options['before'] . $sign : $sign;
  210
+		return self::format($value, $options);
  211
+	}
  212
+
  213
+/**
192 214
  * Alternative number_format() to accommodate multibyte decimals and thousands < PHP 5.4
193 215
  *
194  
- * @param float $number
  216
+ * @param float $value
195 217
  * @param integer $places
196 218
  * @param string $decimals
197 219
  * @param string $thousands
198 220
  * @return string
199 221
  */
200  
-	protected static function _numberFormat($number, $places = 0, $decimals = '.', $thousands = ',') {
  222
+	protected static function _numberFormat($value, $places = 0, $decimals = '.', $thousands = ',') {
201 223
 		if (!isset(self::$_numberFormatSupport)) {
202 224
 			self::$_numberFormatSupport = version_compare(PHP_VERSION, '5.4.0', '>=');
203 225
 		}
204 226
 		if (self::$_numberFormatSupport) {
205  
-			return number_format($number, $places, $decimals, $thousands);
  227
+			return number_format($value, $places, $decimals, $thousands);
206 228
 		}
207  
-		$number = number_format($number, $places, '.', '');
  229
+		$value = number_format($value, $places, '.', '');
208 230
 		$after = '';
209  
-		$foundDecimal = strpos($number, '.');
  231
+		$foundDecimal = strpos($value, '.');
210 232
 		if ($foundDecimal !== false) {
211  
-			$after = substr($number, $foundDecimal);
212  
-			$number = substr($number, 0, $foundDecimal);
  233
+			$after = substr($value, $foundDecimal);
  234
+			$value = substr($value, 0, $foundDecimal);
213 235
 		}
214  
-		while (($foundThousand = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $number)) != $number) {
215  
-			$number = $foundThousand;
  236
+		while (($foundThousand = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $value)) != $value) {
  237
+			$value = $foundThousand;
216 238
 		}
217  
-		$number .= $after;
218  
-		return strtr($number, array(' ' => $thousands, '.' => $decimals));
  239
+		$value .= $after;
  240
+		return strtr($value, array(' ' => $thousands, '.' => $decimals));
219 241
 	}
220 242
 
221 243
 /**
@@ -244,14 +266,14 @@ protected static function _numberFormat($number, $places = 0, $decimals = '.', $
244 266
  *   the number will be wrapped with ( and )
245 267
  * - `escape` - Should the output be htmlentity escaped? Defaults to true
246 268
  *
247  
- * @param float $number
  269
+ * @param float $value
248 270
  * @param string $currency Shortcut to default options. Valid values are
249 271
  *   'USD', 'EUR', 'GBP', otherwise set at least 'before' and 'after' options.
250 272
  * @param array $options
251 273
  * @return string Number formatted as a currency.
252 274
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
253 275
  */
254  
-	public static function currency($number, $currency = 'USD', $options = array()) {
  276
+	public static function currency($value, $currency = 'USD', $options = array()) {
255 277
 		$default = self::$_currencyDefaults;
256 278
 
257 279
 		if (isset(self::$_currencies[$currency])) {
@@ -272,14 +294,14 @@ public static function currency($number, $currency = 'USD', $options = array())
272 294
 		$result = $options['before'] = $options['after'] = null;
273 295
 
274 296
 		$symbolKey = 'whole';
275  
-		if ($number == 0 ) {
276  
-			if ($options['zero'] !== 0 ) {
  297
+		if ($value == 0) {
  298
+			if ($options['zero'] !== 0) {
277 299
 				return $options['zero'];
278 300
 			}
279  
-		} elseif ($number < 1 && $number > -1 ) {
  301
+		} elseif ($value < 1 && $value > -1) {
280 302
 			if ($options['fractionSymbol'] !== false) {
281 303
 				$multiply = intval('1' . str_pad('', $options['places'], '0'));
282  
-				$number = $number * $multiply;
  304
+				$value = $value * $multiply;
283 305
 				$options['places'] = null;
284 306
 				$symbolKey = 'fraction';
285 307
 			}
@@ -288,10 +310,10 @@ public static function currency($number, $currency = 'USD', $options = array())
288 310
 		$position = $options[$symbolKey . 'Position'] != 'after' ? 'before' : 'after';
289 311
 		$options[$position] = $options[$symbolKey . 'Symbol'];
290 312
 
291  
-		$abs = abs($number);
  313
+		$abs = abs($value);
292 314
 		$result = self::format($abs, $options);
293 315
 
294  
-		if ($number < 0 ) {
  316
+		if ($value < 0) {
295 317
 			if ($options['negative'] == '()') {
296 318
 				$result = '(' . $result . ')';
297 319
 			} else {

0 notes on commit 3fa6b96

Please sign in to comment.
Something went wrong with that request. Please try again.