Number format according to Indian Currency

One of the tasks I usually come across while working on some of the more commercial web applications is to display a number in INR i.e Indian National Rupee ( ₹ ) format which makes it really easy to perceive the number as Indian Rupees. Let me show you how to format a number to Indian Rupee format in PHP

function moneyFormatIndia($number) {
   $decimal = (string)($number - floor($number));
        $money = floor($number);
        $length = strlen($money);
        $delimiter = '';
        $money = strrev($money);

        for($i=0;$i<$length;$i++){
            if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$length){
                $delimiter .=',';
            }
            $delimiter .=$money[$i];
        }

        $result = strrev($delimiter);
        $decimal = preg_replace("/0\./i", ".", $decimal);
        $decimal = substr($decimal, 0, 3);

        if( $decimal != '0'){
            $result = $result.$decimal;
        }
    return $result; // writes the final format where $currency is the currency symbol.
}

Leave a Reply