Created
October 8, 2015 20:37
-
-
Save mohsin/6f8f37b79ba85db57375 to your computer and use it in GitHub Desktop.
Convert PHP number to Indian rupee format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function numberToCurrency($num) | |
{ | |
if(setlocale(LC_MONETARY, 'en_IN')) | |
return money_format('%.0n', $num); | |
else { | |
$explrestunits = "" ; | |
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); | |
for($i=0; $i<sizeof($expunit); $i++){ | |
// creates each of the 2's group and adds a comma to the end | |
if($i==0) | |
{ | |
$explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer | |
}else{ | |
$explrestunits .= $expunit[$i].","; | |
} | |
} | |
$thecash = $explrestunits.$lastthree; | |
} else { | |
$thecash = $num; | |
} | |
return '₹ ' . $thecash; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated above function to handle numbers with decimal
https://gist.github.com/hitswa/e7f6ca5a72af2a480193540ad4bb0a75