note 17090 deleted from ref.strings by jimw
| From: | jimw@php.net | Date: | Fri, 14 Dec 2001 21:15:51 +0000 |
| Subject: | note 17090 deleted from ref.strings by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22819@lists.php.net to get a copy of this message | ||
i needed to change a number into currency with properly placed ','. I came up with the
following and figured id post it in case anyone else needed to do the same.
<?
//just a random number
$num = '225135436';
//reverse the number so you can count
//every three digits and insert a ','
$num = strrev($num);
//$insert_comma prevents code from continually entering the first
//if statement after an insert of ',' because it reduces $i back
//to a number that returns 0 from $i % 3
$insert_comma = 0;
for( $i = 0; $i < strlen($num); $i++)
{
if ( ( !($i % 3) ) && ( $insert_comma ) )
{
// time for a comma
$money = $money . ',';
$i--;
$insert_comma = 0;
}
else
{
//just move the next int over
$money = $money . substr( $num, $i, 1 );
$insert_comma = 1;
}
}
$money = $money . "$";
$money = strrev($money);
$money = $money . ".00";
echo strrev($num) . "\n" . $money . "\n";
?>