note 93893 modified in function.number-format by danbrown
| From: | danbrown@php.net | Date: | Mon, 05 Oct 2009 20:42:50 +0000 |
| Subject: | note 93893 modified in function.number-format by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161377@lists.php.net to get a copy of this message | ||
Because I had a lot of problems with number_format, here is a working, simple and small replacement:
<?php
function FormatNumber($number, $decimals = 0, $thousand_separator = ' ',
$decimal_point = '.')
{
$tmp1 = round((float) $number, $decimals);
while (($tmp2 = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $tmp1)) != $tmp1)
$tmp1 = $tmp2;
return strtr($tmp1, array(' ' => $thousand_separator, '.' =>
$decimal_point));
}
echo FormatNumber(-913578.1415, 2, '.', ',') . "\n";
?>
--was--
Because I had a lot of problems with number_format, here is a working, simple and small replacement:
function FormatNumber($number, $decimals = 0, $thousand_separator = ' ',
$decimal_point = '.')
{
$tmp1 = round((float) $number, $decimals);
while (($tmp2 = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $tmp1)) != $tmp1)
$tmp1 = $tmp2;
return strtr($tmp1, array(' ' => $thousand_separator, '.' =>
$decimal_point));
}
echo FormatNumber(-913578.1415, 2, '.', ',') . "\n";
http://php.net/manual/en/function.number-format.php