note 50289 added to function.number-format
| From: | tonywebman at NOSPAM dot telusplanet dot net | Date: | Wed, 23 Feb 2005 21:14:47 +0000 |
| Subject: | note 50289 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85454@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.number-format.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50289
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50289&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50289&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50289&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50289&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50289&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50289&report=yes
Search -- http://master.php.net/manage/user-notes.php