note 85052 deleted from function.money-format by danbrown

From: Date: Tue, 16 Jun 2009 16:01:07 +0000
Subject: note 85052 deleted from function.money-format by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156927@lists.php.net to get a copy of this message
Note Submitter: Pradeep Gond ---- @ swapnet (22-Jul-2008 08:24) Your code will fail to convert 1000000. It will convert to 10,0,000. Only the first token from the split should be int typecasted, as this might have a leading zero. Here is the fixed version: <?php function convertcash($num, $currency){ if(strlen($num)>3){ $lastthree = substr($num, strlen($num)-3, strlen($num)); $restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping. $expunit = str_split($restunits, 2); if(sizeof($expunit) >= 1) { $explrestunits .= (int)$expunit[0].","; // int typecaste to get rid of the possible leading zero. } for($i=1; $i<sizeof($expunit); $i++){ $explrestunits .= $expunit[$i].","; // creates each of the 2's group and adds a comma to the end } $thecash = $explrestunits.$lastthree; } else { $thecash = $convertnum; } return $currency.$thecash.".00"; // writes the final format where $currency is the currency symbol. } ?>

« previous php.notes (#156927) next »