note 71919 added to function.number-format
| From: | lathem at gibsoncomputer dot net | Date: | Fri, 22 Dec 2006 17:41:53 +0000 |
| Subject: | note 71919 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120855@lists.php.net to get a copy of this message | ||
I've discovered that the commas which number_format() inserts as a thousands separators causes
math functions to break.
Bit of a newbie thing, but it bit me in the ass.
eg--
<?
$foo = number_format(1234, 2);
?>
$foo is now equal to 1,234.00
<?
$bar = 23;
$result = $foo+$bar;
?>
One would think $result == 1,257.00. However, because number_format added a comma to $foo, the
expression will not evaluate correctly.
Instead, do things in this order:
<?
$foo=1234;
$bar=23;
$foobar=number_format($foo+$bar, 2);
?>
$foobar is now set to 1,257.00.
Another method, if you're fixing code you've already written and don't want to change
a huge amount, is to remove the thousands separator, using the appropriate variable to
number_format.
<?
$foo = number_format(1234, 2, '.', '');
$bar = number_format(23, 2, '.', '');
$foobar = number_format($foo+$bar, 2);
?>
$foobar will now be set to 1,257.00
----
Server IP: 64.71.164.2
Probable Submitter: 72.174.26.8
----
Manual Page -- http://www.php.net/manual/en/function.number-format.php
Edit -- https://master.php.net/note/edit/71919
Del: integrated -- https://master.php.net/note/delete/71919/integrated
Del: useless -- https://master.php.net/note/delete/71919/useless
Del: bad code -- https://master.php.net/note/delete/71919/bad+code
Del: spam -- https://master.php.net/note/delete/71919/spam
Del: non-english -- https://master.php.net/note/delete/71919/non-english
Del: in docs -- https://master.php.net/note/delete/71919/in+docs
Del: other reasons-- https://master.php.net/note/delete/71919
Reject -- https://master.php.net/note/reject/71919
Search -- https://master.php.net/manage/user-notes.php