note 92258 deleted from function.number-format by danbrown
| From: | danbrown@php.net | Date: | Thu, 16 Jul 2009 14:23:54 +0000 |
| Subject: | note 92258 deleted from function.number-format by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158368@lists.php.net to get a copy of this message | ||
Note Submitter: nothing at everything dot com
----
Just changing formatMoney to trunc money to 2 decimal places and preventing rounding
off....Something we dont want when dealing with currency...
function formatMoney($number, $cents = 2) { // cents: 0=never, 1=if needed, 2=always
if (is_numeric($number)) { // a number
if (!$number) { // zero
$money = ($cents == 2 ? '0.00' : '0'); // output zero
} else { // value
if (floor($number) == $number) { // whole number
$money = number_format($number, ($cents == 2 ? 2 : 0)); // format
} else { // cents
//$money = number_format(round($number, 2), ($cents == 0 ? 0 : 2)); // format
$money_value = intval($number * 100 )/100;
$money = number_format($money_value, ($cents == 0 ? 0 : 2));
} // integer or decimal
} // value
return $money;
} // numeric
} // formatMoney