note 102876 added to function.money-format
| From: | Subhendu at SubhenduSen dot com | Date: | Fri, 11 Mar 2011 18:21:13 +0000 |
| Subject: | note 102876 added to function.money-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177802@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 59.184.186.4
----
Manual Page -- http://www.php.net/manual/en/function.money-format.php
Edit -- https://master.php.net/note/edit/102876
Del: integrated -- https://master.php.net/note/delete/102876/integrated
Del: useless -- https://master.php.net/note/delete/102876/useless
Del: bad code -- https://master.php.net/note/delete/102876/bad+code
Del: spam -- https://master.php.net/note/delete/102876/spam
Del: non-english -- https://master.php.net/note/delete/102876/non-english
Del: in docs -- https://master.php.net/note/delete/102876/in+docs
Del: other reasons-- https://master.php.net/note/delete/102876
Reject -- https://master.php.net/note/reject/102876
Search -- https://master.php.net/manage/user-notes.php