note 74826 deleted from function.substr by nlopess

From: Date: Tue, 01 May 2007 13:44:30 +0000
Subject: note 74826 deleted from function.substr by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126056@lists.php.net to get a copy of this message
Note Submitter: liamtoh6 at hotmail.com ---- I changed the fucntion created by chislam4444 at gmail dot com so that it is dynamic and the code is shorter. <? function add_commas($num){ $decimalpos = strpos($num, "."); if(!$decimalpos === false){ $startpos = $decimalpos; } else { $startpos = strlen($num); } $comma_string = $num; $startpos -= 3; for($a = $startpos; $a > 0; $a -= 3){ $comma_string = substr($comma_string, 0, $a) . "," . substr($comma_string, $a); } return $comma_string; } echo add_commas(30000); // 30,000 echo "<br>"; echo add_commas(300000); // 300,000 echo "<br>"; echo add_commas(3000000); // 3,000,000 ?>

« previous php.notes (#126056) next »