Convert number to rupees in PHP function to convert number to word
PHP

Convert number to rupees in PHP – PHP function to convert number to word

Convert number to rupees in PHP – PHP function to convert number to word. आज के इस tutorial में हम सीखेंगे की किसी भी number को PHP function के द्वारा words में कैसे convert करें जैसे की 501.23 Rs number में लिखा हुआ है और इसको word में convert करना है like “Five hundred and one rupees twenty three paise”.

Convert number to rupees in PHP – PHP function to convert number to word

function getIndianCurrency(float $number)
    {
        $no = floor($number);
        $decimal = round($number - $no, 2) * 100;
        $decimal_part = $decimal;
        $hundred = null;
        $hundreds = null;
        $digits_length = strlen($no);
        $decimal_length = strlen($decimal);
        $i = 0;
        $str = array();
        $str2 = array();
        $words = array(0 => '', 1 => 'one', 2 => 'two',
            3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
            7 => 'seven', 8 => 'eight', 9 => 'nine',
            10 => 'ten', 11 => 'eleven', 12 => 'twelve',
            13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
            16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen',
            19 => 'nineteen', 20 => 'twenty', 30 => 'thirty',
            40 => 'forty', 50 => 'fifty', 60 => 'sixty',
            70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
        $digits = array('', 'hundred','thousand','lakh', 'crore');
        
        while( $i < $digits_length ) {
            $divider = ($i == 2) ? 10 : 100;
            $number = floor($no % $divider);
            $no = floor($no / $divider);
            $i += $divider == 10 ? 1 : 2;
            if ($number) {
                $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
                $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
                $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
            } else $str[] = null;
        }

        $d = 0;
        while( $d < $decimal_length ) {
            $divider = ($d == 2) ? 10 : 100;
            $decimal_number = floor($decimal % $divider);
            $decimal = floor($decimal / $divider);
            $d += $divider == 10 ? 1 : 2;
            if ($decimal_number) {
                $plurals = (($counter = count($str2)) && $decimal_number > 9) ? 's' : null;
                $hundreds = ($counter == 1 && $str2[0]) ? ' and ' : null;
                @$str2 [] = ($decimal_number < 21) ? $words[$decimal_number].' '. $digits[$decimal_number]. $plural.' '.$hundred:$words[floor($decimal_number / 10) * 10].' '.$words[$decimal_number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
            } else $str2[] = null;
        }
        
        $Rupees = implode('', array_reverse($str));
        $paise = implode('', array_reverse($str2));
        $paise = ($decimal_part > 0) ? $paise . ' Paise' : '';
        return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
    }

Function Calling

getIndianCurrency(501.23);

Code Description in details

इस code में मैंने सबसे पहले दिए गए number को दो part में अलग अलग divide किया मतलब की integer part को अलग और decimal (दशमलव) part को अलग अलग variable में store किया|

Rupees part को store करने के लिए मैंने $no variable का इस्तेमाल किया और पैसे (दशमलव के बाद के) part को store करने के लिए मैंने $decimal variable का इस्तेमाल किया| यहाँ floor() method के द्वारा हम integer part मतलब की rupees part को अलग कर रहे हैं|

उसके बाद मैंने दोनों number का length find किया और उसको अलग अलग variable में store कर दिया|

अब एक array बनाया जिसमें 0 से लेकर के 100 तक के number को word में लिखा और उसके बाद के array में 100 से ऊपर आने वाले words को लिखा|उसके बाद मैंने दो while loop अलग अलग execute कराया पहला while loop rupees part को words में convert करने के लिए और दूसरा while loop paise part को words में convert करने के लिए|

Loop के अन्दर हर number को Modulo division करके एक एक number के अनुसार उसका place और उसका word को find किया और उसको array में store करते गया| सबसे अंत में उस array को reverse order में implode करके value को return कर दिया|

इसे भी पढ़ें:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.