Skip to content

Instantly share code, notes, and snippets.

@rupakraj
Created July 13, 2014 03:11
Show Gist options
  • Save rupakraj/2ff589c2cc3fc23c8ad3 to your computer and use it in GitHub Desktop.
Save rupakraj/2ff589c2cc3fc23c8ad3 to your computer and use it in GitHub Desktop.
Php function to format the money in Rs. (Nepali)
function money_format_nep($number, $is_unicode=true)
{
//number_format($number,".");
if(strstr($number,"-"))
{
$number = str_replace("-","",$number);
$negative = "-";
}
$split_number = @explode(".",$number);
$rupee = $split_number[0];
$paise = @$split_number[1];
if(@strlen($rupee)>3)
{
$hundreds = substr($rupee,strlen($rupee)-3);
$thousands_in_reverse = strrev(substr($rupee,0,strlen($rupee)-3));
for($i=0; $i<(strlen($thousands_in_reverse)); $i=$i+2)
{
$thousands .= $thousands_in_reverse[$i].$thousands_in_reverse[$i+1].",";
}
$thousands = strrev(trim($thousands,","));
$formatted_rupee = $thousands.",".$hundreds;
}
else
{
$formatted_rupee = $rupee;
}
if((int)$paise>0)
{
$formatted_paise = ".".substr($paise,0,2);
}
else
{
$formatted_paise = ".".substr("00",0,2);
}
if($is_unicode){
return get_nep($negative.$formatted_rupee.$formatted_paise);
}
else{
return $negative.$formatted_rupee.$formatted_paise;
}
}
function get_nep($eng_str){
$replace = array("१","२","३","४","५","६","७","८","९","०" );
$find = array("1","2","3","4","5","6","7","8","9","0" );
$nep_str = str_replace($find, $replace, $eng_str);
return $nep_str;
}
// calling
// if you need the number format in unicode format then call using
money_format_nep("12121244",true);
// if you dont need unicode characters then just pass false
money_format_nep("12121244",false);
@codersuraz
Copy link

For people who don't need to use the Unicode. Simply use this snippet.

format_money($number) {
 $formatter = new \NumberFormatter('en_IN', \NumberFormatter::CURRENCY);
 return str_replace('₹', 'RS. ', $formatter->format($number));
}

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