Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hitswa/e7f6ca5a72af2a480193540ad4bb0a75 to your computer and use it in GitHub Desktop.
Save hitswa/e7f6ca5a72af2a480193540ad4bb0a75 to your computer and use it in GitHub Desktop.
numbertoinr.php
<?php
function numberToCurrency($number)
{
if(setlocale(LC_MONETARY, 'en_IN'))
return money_format('%.0n', $number);
else {
$explrestunits = "" ;
$number = explode('.', $number);
$num = $number[0];
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;
}
if(!empty($number[1])) {
if(strlen($number[1]) == 1) {
return '₹ ' .$thecash . '.' . $number[1] . '0';
} else if(strlen($number[1]) == 2){
return '₹ ' .$thecash . '.' . $number[1];
} else {
return 'cannot handle decimal values more than two digits...';
}
} else {
return '₹ ' .$thecash.'.00';
}
}
}
?>
@thatal
Copy link

thatal commented Oct 8, 2018

I have added dynamic decimal place. but there is a problem how to round off the decimal number.

function numberToCurrency($number, $decimal = 2) {
    // window is not supported money_format
    if(setlocale(LC_MONETARY, 'en_IN'))
         return money_format('%!'.$decimal.'n', $number);
     else {
        $explrestunits = "" ;
        $decimal_text   = "" ;
        $number = explode('.', $number);
        $num = $number[0];
        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;
        }
        for($i=0; $i<$decimal; $i++){
            $decimal_text .= "0";
        }
        if(!empty($number[1])) {
            // if(strlen($number[1]) == 1) {
            //     return $thecash . '.' . $number[1] . '0';
            //     // return '₹ ' .$thecash . '.' . $number[1] . '0';
            // } else if(strlen($number[1]) == 2){
            //     return $thecash . '.' . $number[1];
            //     // return '₹ ' .$thecash . '.' . $number[1];
            // } else {
            //     return $thecash . '.' . $number[1];
            //     // return 'cannot handle decimal values more than two digits...';
            // }
            if(strlen($number[1]) > $decimal){
              // round off decimal number 
                $decimal_text = substr($number[1], 0, $decimal);
            }elseif (strlen($number[1]) < $decimal) {
                $decimal_text = $number[1].substr($decimal_text, 0, ($decimal_text-strlen($number[1])));
            }
            return $thecash.$decimal_text;
        } else {
            return $thecash.$decimal_text;
            // return '₹ ' .$thecash.'.00';
        }
    }
}

@koushikbasu
Copy link

I think this simple and efficient:-
function formatToInr($number){
$number=round($number,2);
// windows is not supported money_format
if(setlocale(LC_MONETARY, 'en_IN')){
return money_format('%!'.$decimal.'n', $number);
}
else {
if(floor($number) == $number) {
$append='.00';
}else{
$append='';
}
$number = preg_replace("/(\d+?)(?=(\d\d)+(\d)(?!\d))(.\d+)?/i", "$1,", $number);
return $number.$append;
}
}

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