note 50289 deleted from function.number-format by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:15:25 +0000 |
| Subject: | note 50289 deleted from function.number-format by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196945@lists.php.net to get a copy of this message | ||
Note Submitter: tonywebman at NOSPAM dot telusplanet dot net
----
While trying to add variables whose values had been processed with number_format() I found an
interesting gotcha. Perhaps this might help others.
Since number_format() returns a string, numbers returned that DO NOT have a comma in them will still
be added but numbers that DO have a comma will not be added because PHP considers them a string and
ignores them.
e.g. #1
$quant_mag = 1;
$cost_mag = 100;
$quant_ffr = 1
$cost_ffr = 100;
$ext_mag = number_format($quant_mag * $cost_mag,2);
$ext_ffr = number_format($quant_ffr * $cost_ffr,2);
$total_cost = $ext_mag + $ext_ffr;
// $total cost is: 200
e.g. #2
$quant_mag = 10;
$cost_mag = 100;
$quant_ffr = 1
$cost_ffr = 100;
$ext_mag = number_format($quant_mag * $cost_mag,2);
$ext_ffr = number_format($quant_ffr * $cost_ffr,2);
$total_cost = $ext_mag + $ext_ffr;
// $total cost is: 100 (not 1100 as you would expect) because $ext_mag is ignored because php
interprets its value (1,000) as a string so it won't add it to $ext_ffr.