WHAT'S NEW?
Loading...

How to convert number to Indian Money format



indian_money

            This Example illustrates how number can be converted to money format. Sometimes we must display number as currency and for this purpose PHP provides `money_format` function and it works in indian format fine till ten thousand ( five digit numbers )but in more than 5 digits, it fails for Indian Currency.

So, Here we will use user define function look like below.


index.php

<?php
function custom_format($n, $d = 0) {
    $n = number_format($n, $d, '.', '');
    $n = strrev($n);

    if ($d) $d++;
    $d += 3;

    if (strlen($n) > $d)
        $n = substr($n, 0, $d) . ','
           . implode(',', str_split(substr($n, $d), 2));
    return strrev($n);
}

// Call Function with number agrument
echo custom_format(1234567890);       // 1,23,45,67,890
echo custom_format(1234567890, 2);    // 1,23,45,67,890.00
echo custom_format(1234567890.11);    // 1,23,45,67,890
echo custom_format(1234567890.11, 2); // 1,23,45,67,890.11
?>


Here we create function `custom_format` with numeric  argument. That’s it we got number in Currency format. 





0 comments:

Post a Comment