Its been real long since I updated this space. And instead of adding something for the heck of it, I thought of posting something really useful. This is a piece of PHP code that returns an amount/number in words. This is in English, but you can easily translate it to your own. Hope this helps!
This reminds me of Semester 1 assignment given by Shalini Puri ma’am to determine the optimal number of currency coins & notes required to put together a specified amount. Good old days!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
class Money { // Uses the Thousand, Lakh, Crore format CONST ThouLakhCrore = 0; // Uses the Thousand, Million, Billion format CONST ThouMilBil = 1; public static function amountToWords($no, $currency='INR', $formatString = '%UNIT% %WHOLE% & %FRACTION% %SUBUNIT%', $option = self::ThouLakhCrore) { if ($no == 0) return "Zero"; $words = $formatString; $whole = ''; $fraction = ''; $wholeInt = floor($no); $whole = ($wholeInt == 0) ? 'Zero' : self::words_big($wholeInt, $option); $fracInt = (int) (($no - $wholeInt) * 100) % 100; $fraction = ($fracInt == 0) ? 'Zero' : self::words_tens($fracInt); // TBD: Lookup unit, subunit for $currency & replace values below $curr_unit = 'Rs.'; $curr_subunit = 'paise'; $words = str_replace('%WHOLE%', $whole, $words); $words = str_replace('%FRACTION%', $fraction, $words); $words = str_replace('%UNIT%', $curr_unit, $words); $words = str_replace('%SUBUNIT%', $curr_subunit, $words); return ucfirst($words); } protected static function words_big($no, $option) { $descSingular = $option == 0 ? array("Thousand", "Lakh", "Crore", "Thousand Crore") : array("Thousand", "Million", "Billion", "Trillion"); $descPlural = $option == 0 ? array("Thousand", "Lakhs", "Crores", "Thousand Crores") : array("Thousand", "Million", "Billion", "Trillion"); $compare = $option == 0 ? array(1000, 100000, 10000000, 10000000000) : array(1000, 1000000, 1000000000, 1000000000000); $divide = count($descSingular); $loop = $divide; $words = ''; /* handle upto thousand */ for ($i = $loop - 1; $i >= 0; $i--) { $nos = ($i == $loop - 1) ? $no : $no % $compare[$i + 1]; $split = (int) floor($nos / $compare[$i]); if ($split > 0) { if ($split < 100) $words .= ' ' . self::words_tens($split) . ' ' . ($split == 1 ? $descSingular[$i] : $descPlural[$i]); if ($split >= 100 && $split < 1000) $words .= ' ' . self::words_hundreds($split) . ' ' . $descPlural[$i]; else if ($split >= 1000) $words .= ' ' . self::words_big($split, $option) . ' ' . $descPlural[$i]; } if ($i == 0) { $hundred = (int) $no % 1000; if ($hundred > 0 && $hundred < 999) $words .= ' ' . self::words_hundreds($hundred); } } return trim($words); } protected static function words_hundreds($no) { $words = ''; if ($no > 999) throw new Exception("Invalid call to hundreds function"); $hundreds = 0; if ($no > 99) { $hundreds = floor($no / 100); $words = self::words_units($hundreds) . ' ' . "Hundred"; } $words .= ' ' . self::words_tens($no - (100 * $hundreds)); return trim($words); } protected static function words_tens($no) { $words = ''; if ($no > 99) throw new Exception("Invalid call to Tens function"); $tens = floor($no / 10); if ($tens > 1) { switch ($tens) { case 2: $words = "Twenty"; break; case 3: $words = "Thirty"; break; case 4: $words = "Forty"; break; case 5: $words = "Fifty"; break; case 6: $words = "Sixty"; break; case 7: $words = "Seventy"; break; case 8: $words = "Eighty"; break; case 9: $words = "Ninety"; break; default: break; } $words .= ' ' . self::words_units($no - $tens * 10); } else $words = self::words_units($no); return trim($words); } protected static function words_units($no) { $words = ''; if ($no > 19) throw new Exception("Invalid call to Units function"); switch ($no) { case 1: $words = "One"; break; case 2: $words = "Two"; break; case 3: $words = "Three"; break; case 4: $words = "Four"; break; case 5: $words = "Five"; break; case 6: $words = "Six"; break; case 7: $words = "Seven"; break; case 8: $words = "Eight"; break; case 9: $words = "Nine"; break; case 10: $words = "Ten"; break; case 11: $words = "Eleven"; break; case 12: $words = "Twelve"; break; case 13: $words = "Thirteen"; break; case 14: $words = "Fourteen"; break; case 15: $words = "Fifteen"; break; case 16: $words = "Sixteen"; break; case 17: $words = "Seventeen"; break; case 18: $words = "Eighteen"; break; case 19: $words = "Nineteen"; break; default: break; } return $words; } } |