note 102876 deleted from function.money-format by danbrown

From: Date: Fri, 11 Mar 2011 18:32:56 +0000
Subject: note 102876 deleted from function.money-format by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177803@lists.php.net to get a copy of this message
Note Submitter: Subhendu at SubhenduSen dot com ---- Indian Money Format: @swapnet : Your function is generating error. Moreover, it doesn't allow Paisa (decimal). So I changed it a bit: function convertcash($number){ list($num,$decimal)=explode('.', $number); 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 = $num; } if ($decimal) { $cash = $thecash . "." . $decimal; } else { $cash = $thecash . ".00"; } return $cash; // writes the final format where $currency is the currency symbol. } $amount="12345678"; convertcash($amount); will produce 1,23,45,678.00 $amount="12345678.12"; convertcash($amount); will produce 1,23,45,678.12 Thanks for your initial function though. Hope this helps.

« previous php.notes (#177803) next »