php number to words

Solutions on MaxInterview for php number to words by the best coders in the world

showing results for - "php number to words"
Simone
11 Aug 2016
1//This if function to convert numbers to Indian Currency
2
3function getIndianCurrency(float $number)
4{
5    $decimal = round($number - ($no = floor($number)), 2) * 100;
6    $hundred = null;
7    $digits_length = strlen($no);
8    $i = 0;
9    $str = array();
10    $words = array(0 => '', 1 => 'one', 2 => 'two',
11        3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
12        7 => 'seven', 8 => 'eight', 9 => 'nine',
13        10 => 'ten', 11 => 'eleven', 12 => 'twelve',
14        13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
15        16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen',
16        19 => 'nineteen', 20 => 'twenty', 30 => 'thirty',
17        40 => 'forty', 50 => 'fifty', 60 => 'sixty',
18        70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
19    $digits = array('', 'hundred','thousand','lakh', 'crore');
20    while( $i < $digits_length ) {
21        $divider = ($i == 2) ? 10 : 100;
22        $number = floor($no % $divider);
23        $no = floor($no / $divider);
24        $i += $divider == 10 ? 1 : 2;
25        if ($number) {
26            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
27            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
28            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
29        } else $str[] = null;
30    }
31    $Rupees = implode('', array_reverse($str));
32    $paise = ($decimal > 0) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
33    return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
34}