Skip to content

Instantly share code, notes, and snippets.

@mohsin
Created October 8, 2015 20:37
Show Gist options
  • Save mohsin/6f8f37b79ba85db57375 to your computer and use it in GitHub Desktop.
Save mohsin/6f8f37b79ba85db57375 to your computer and use it in GitHub Desktop.
Convert PHP number to Indian rupee format
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;
}
}
@hitswa
Copy link

hitswa commented Apr 10, 2018

updated above function to handle numbers with decimal

https://gist.github.com/hitswa/e7f6ca5a72af2a480193540ad4bb0a75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment