note 32708 modified in function.sprintf by jmcastagnetto
| From: | jmcastagnetto@php.net | Date: | Mon, 16 Jun 2003 15:56:25 +0000 |
| Subject: | note 32708 modified in function.sprintf by jmcastagnetto | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-50434@lists.php.net to get a copy of this message | ||
[Editor's note: Usually it will be simpler to use number_format() to generate the type of
output the code below produces. The built-in function would be faster than using a regular
expression.]
here is a simple function to add commas to a number.
function comma_format($num){
$number = sprintf("%01.2f", $num);
while (preg_match("!(-?\d+)(\d\d\d)!", $number))
{
$number = preg_replace("!(-?\d+)(\d\d\d)!","$1,$2", $number);
}
return $number;
}
--was--
here is a simple function to add commas to a number.
function comma_format($num){
$number = sprintf("%01.2f", $num);
while (preg_match("!(-?\d+)(\d\d\d)!", $number))
{
$number = preg_replace("!(-?\d+)(\d\d\d)!","$1,$2", $number);
}
return $number;
}
http://www.php.net/manual/en/function.sprintf.php