phpcurrencynumber-formatting

Formatting price in INR method


I have a piece of code, which formats a number to INR Price (₹1.00/- , ₹10,00,000.00/- , etc.):

function price_format($num,$type = 1){
    $num_full = number_format($num,2);
    if (strpos($num, '.') !== false) {
    $num = substr($num_full, 0, strpos($num_full, "."));
    }
    
    if($type == 1){ // '₹10,00,000.00/-'
        $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.substr($num_full, -3, strpos($num_full, "."))."/-";
    } else {
        $thecash = "₹".$num.substr($num_full, -3, strpos($num_full, "."))."/-";
    }
    return $thecash; // writes the final format where $currency is the currency symbol.
    }
}

For the below test, it is not giving as expected result.

echo price_format(1)."<br />";
echo price_format(10)."<br />";
echo price_format(84289.35);

Live Demo

Also, the code is open and I am happy if someone wants to have this code more clear and short


Solution

  • There is no use of strpos while setting the value to $thecash. strpos doc

    Use number_format() as: number_format((float)$num,2,'.','');

    Working Code:-

    function price_format($num,$type = 1){
        $num_full = number_format((float)$num,2,'.','');
        if (strpos($num, '.') !== false) {
        $num = substr($num_full, 0, strpos($num_full, "."));
        }
        
        if($type == 1){ // '₹10,00,000.00/-'
            $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.substr($num_full, -3)."/-";
        } else {
            $thecash = "₹".$num.substr($num_full, -3)."/-";
        }
        return $thecash; // writes the final format where $currency is the currency symbol.
        }
    }
    
    echo price_format(1).PHP_EOL;
    echo price_format(10).PHP_EOL;
    echo price_format(84289.35);