Is there an easy way to convert a number to a word in PHP?
在PHP中,是否有一种简单的方法可以将数字转换为单词?例如,27到27。
我在网上找到了一些(2007/2008)源代码,因为它是版权所有,但我可以自由使用它,并根据自己的需要修改它,所以我将它放在这里,并在cc wiki下重新授权:
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | <?php /** * English Number Converter - Collection of PHP functions to convert a number * into English text. * * This exact code is licensed under CC-Wiki on Stackoverflow. * http://creativecommons.org/licenses/by-sa/3.0/ * * @link http://stackoverflow.com/q/277569/367456 * @question Is there an easy way to convert a number to a word in PHP? * * This file incorporates work covered by the following copyright and * permission notice: * * Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text * You can use this freely and modify it however you want. */ function convertNumber($number) { list($integer, $fraction) = explode(".", (string) $number); $output =""; if ($integer{0} =="-") { $output ="negative"; $integer = ltrim($integer,"-"); } else if ($integer{0} =="+") { $output ="positive"; $integer = ltrim($integer,"+"); } if ($integer{0} =="0") { $output .="zero"; } else { $integer = str_pad($integer, 36,"0", STR_PAD_LEFT); $group = rtrim(chunk_split($integer, 3,""),""); $groups = explode("", $group); $groups2 = array(); foreach ($groups as $g) { $groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2}); } for ($z = 0; $z < count($groups2); $z++) { if ($groups2[$z] !="") { $output .= $groups2[$z] . convertGroup(11 - $z) . ( $z < 11 && !array_search('', array_slice($groups2, $z + 1, -1)) && $groups2[11] != '' && $groups[11]{0} == '0' ?" and" :"," ); } } $output = rtrim($output,","); } if ($fraction > 0) { $output .=" point"; for ($i = 0; $i < strlen($fraction); $i++) { $output .="" . convertDigit($fraction{$i}); } } return $output; } function convertGroup($index) { switch ($index) { case 11: return" decillion"; case 10: return" nonillion"; case 9: return" octillion"; case 8: return" septillion"; case 7: return" sextillion"; case 6: return" quintrillion"; case 5: return" quadrillion"; case 4: return" trillion"; case 3: return" billion"; case 2: return" million"; case 1: return" thousand"; case 0: return""; } } function convertThreeDigit($digit1, $digit2, $digit3) { $buffer =""; if ($digit1 =="0" && $digit2 =="0" && $digit3 =="0") { return""; } if ($digit1 !="0") { $buffer .= convertDigit($digit1) ." hundred"; if ($digit2 !="0" || $digit3 !="0") { $buffer .=" and"; } } if ($digit2 !="0") { $buffer .= convertTwoDigit($digit2, $digit3); } else if ($digit3 !="0") { $buffer .= convertDigit($digit3); } return $buffer; } function convertTwoDigit($digit1, $digit2) { if ($digit2 =="0") { switch ($digit1) { case"1": return"ten"; case"2": return"twenty"; case"3": return"thirty"; case"4": return"forty"; case"5": return"fifty"; case"6": return"sixty"; case"7": return"seventy"; case"8": return"eighty"; case"9": return"ninety"; } } else if ($digit1 =="1") { switch ($digit2) { case"1": return"eleven"; case"2": return"twelve"; case"3": return"thirteen"; case"4": return"fourteen"; case"5": return"fifteen"; case"6": return"sixteen"; case"7": return"seventeen"; case"8": return"eighteen"; case"9": return"nineteen"; } } else { $temp = convertDigit($digit2); switch ($digit1) { case"2": return"twenty-$temp"; case"3": return"thirty-$temp"; case"4": return"forty-$temp"; case"5": return"fifty-$temp"; case"6": return"sixty-$temp"; case"7": return"seventy-$temp"; case"8": return"eighty-$temp"; case"9": return"ninety-$temp"; } } } function convertDigit($digit) { switch ($digit) { case"0": return"zero"; case"1": return"one"; case"2": return"two"; case"3": return"three"; case"4": return"four"; case"5": return"five"; case"6": return"six"; case"7": return"seven"; case"8": return"eight"; case"9": return"nine"; } } |
或者,可以在PHP中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php if ($argc < 3) { echo"usage: php {$argv[0]} lang-tag number ... "; exit; } array_shift($argv); $lang_tag = array_shift($argv); $nf1 = new NumberFormatter($lang_tag, NumberFormatter::DECIMAL); $nf2 = new NumberFormatter($lang_tag, NumberFormatter::SPELLOUT); foreach ($argv as $num) { echo $nf1->format($num).' is '.$nf2->format($num)." "; } |
pecl中有
- BG(保加利亚语)作者:Kouber Saparev
- C(捷克)佩特'佩帕'帕维尔
- Piotr Klaban的de(德语)
- 丹麦文(Jesper Veggerby)
- Piotr Klaban的En_100(Donald Knuth System,英语)
- Piotr Klaban的en_GB(英国英语)
- Piotr Klaban的en ou US(美式英语)
- Xavier Noguer的ES(西班牙Castellano)
- 马丁马尔泽的《阿根廷西班牙语》
- Erkki Saarniit的ET(爱沙尼亚语)
- 法国(Kouber Saparev)
- Kouber Saparev和Philippe Bajoit的《法国比利时》
- 他(希伯来语)哈达波拉特
- 胡胡(匈牙利语),尼尔斯·霍姆著
- ID(印度尼西亚)作者:Ernas M.Jamil和Arif Rifai Dwiyanto
- 它是菲利波·贝尔特拉米尼和戴维德·凯罗尼写的(意大利语)
- 劳里纳斯·布特库斯的《立陶宛战争》
- 荷兰荷兰语(Wham van Dinter)
- Piotr Klaban的PL(波兰语)
- 巴西人葡萄牙语,作者:Marcelo Subtil Marcal和Mario H.C.T.
- 安德烈·德米涅夫(俄罗斯)
- 瑞典文(Robin Ericsson)
使用NumberFormatter类很容易转换为单词。
1 2 3 4 5 6 7 8 9 10 11 | <?php $number = '12345'; $locale = 'en_US'; $fmt = numfmt_create($locale, NumberFormatter::SPELLOUT); $in_words = numfmt_format($fmt, $number); print_r($in_words); // twelve thousand three hundred forty-five ?> |
我需要一个将"and"放入返回的字符串并将其格式化为一个句子的解决方案——通常就像人类所说的那样。所以我修改了一个不同的解决方案,稍微贴了一下,因为我认为这对某人有用。
1 | 4,835,301 returns"Four million eight hundred and thirty five thousand three hundred and one." |
代码
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 | function convertNumber($num = false) { $num = str_replace(array(',', ''), '' , trim($num)); if(! $num) { return false; } $num = (int) $num; $words = array(); $list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ); $list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred'); $list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' ); $num_length = strlen($num); $levels = (int) (($num_length + 2) / 3); $max_length = $levels * 3; $num = substr('00' . $num, -$max_length); $num_levels = str_split($num, 3); for ($i = 0; $i < count($num_levels); $i++) { $levels--; $hundreds = (int) ($num_levels[$i] / 100); $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ( $hundreds == 1 ? '' : '' ) . ' ' : ''); $tens = (int) ($num_levels[$i] % 100); $singles = ''; if ( $tens < 20 ) { $tens = ($tens ? ' and ' . $list1[$tens] . ' ' : '' ); } elseif ($tens >= 20) { $tens = (int)($tens / 10); $tens = ' and ' . $list2[$tens] . ' '; $singles = (int) ($num_levels[$i] % 10); $singles = ' ' . $list1[$singles] . ' '; } $words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' ); } //end for loop $commas = count($words); if ($commas > 1) { $commas = $commas - 1; } $words = implode(' ', $words); $words = preg_replace('/^\s\b(and)/', '', $words ); $words = trim($words); $words = ucfirst($words); $words = $words ."."; return $words; } |
我重写了上面的代码,以适应标准的美国书面字数格式。
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 | function singledigit($number){ switch($number){ case 0:$word ="zero";break; case 1:$word ="one";break; case 2:$word ="two";break; case 3:$word ="three";break; case 4:$word ="four";break; case 5:$word ="five";break; case 6:$word ="six";break; case 7:$word ="seven";break; case 8:$word ="eight";break; case 9:$word ="nine";break; } return $word; } function doubledigitnumber($number){ if($number == 0){ $word =""; } else{ $word ="-".singledigit($number); } return $word; } function doubledigit($number){ switch($number[0]){ case 0:$word = doubledigitnumber($number[1]);break; case 1: switch($number[1]){ case 0:$word ="ten";break; case 1:$word ="eleven";break; case 2:$word ="twelve";break; case 3:$word ="thirteen";break; case 4:$word ="fourteen";break; case 5:$word ="fifteen";break; case 6:$word ="sixteen";break; case 7:$word ="seventeen";break; case 8:$word ="eighteen";break; case 9:$word ="ninteen";break; }break; case 2:$word ="twenty".doubledigitnumber($number[1]);break; case 3:$word ="thirty".doubledigitnumber($number[1]);break; case 4:$word ="forty".doubledigitnumber($number[1]);break; case 5:$word ="fifty".doubledigitnumber($number[1]);break; case 6:$word ="sixty".doubledigitnumber($number[1]);break; case 7:$word ="seventy".doubledigitnumber($number[1]);break; case 8:$word ="eighty".doubledigitnumber($number[1]);break; case 9:$word ="ninety".doubledigitnumber($number[1]);break; } return $word; } function unitdigit($numberlen,$number){ switch($numberlen){ case 3:case 6:case 9:case 12:$word ="hundred";break; case 4:case 5:$word ="thousand";break; case 7:case 8:$word ="million";break; case 10:case 11:$word ="billion";break; } return $word; } function numberToWord($number){ $numberlength = strlen($number); if ($numberlength == 1) { return singledigit($number); }elseif ($numberlength == 2) { return doubledigit($number); } else { $word =""; $wordin =""; switch ($numberlength ) { case 5:case 8: case 11: if($number[0] >0){ $unitdigit = unitdigit($numberlength,$number[0]); $word = doubledigit($number[0].$number[1]) ."".$unitdigit.""; return $word."".numberToWord(substr($number,2)); } else{ return $word."".numberToWord(substr($number,1)); } break; default: if($number[0] >0){ $unitdigit = unitdigit($numberlength,$number[0]); $word = singledigit($number[0]) ."".$unitdigit.""; } return $word."".numberToWord(substr($number,1)); } } } |
可以使用NumberFormatter类:
1 2 | $f = new NumberFormatter("en", NumberFormatter::SPELLOUT); echo $f->format($myNumber); |
是的。如果不使用库,您只需遵循这一点。
首先,如果php.ini中启用了
带wamp的intl扩展php_intl.dll
成功移动之后,所有文件都从ICU开始。
发件人:
致:
然后重新启动服务器。
尝试运行此代码:
1 2 | $f = new NumberFormatter("en", NumberFormatter::SPELLOUT); echo $f->format(123456); |
它将显示十二万三千四百五十六的产量。
希望这对每个人都有帮助。
这是我今晚写的一个小班。Caveats:
因此,这个类将把数字转换成
用途:
1 2 | $number = '999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'; reallyBig::longform($number); |
可选的第二个布尔参数默认为"真",在正确的位置尽可能添加逗号,以使数字更具可读性。
顺便说一下,如果你想让一个
其他方法不需要使用
我停在999百分位的唯一原因是,百分位是我在网站上看到的最后一个数字,当时我记不清在一个百分位之后发生了什么。
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | class reallyBig { private static $map, $strings; private static function map() { $map = array(); $num = 1; $count = 1; while($num < 307) { if($count == 1) $map[$num] = $num+2; elseif($count == 2) $map[$num] = $num+1; else { $map[$num] = $num; $count = 0; } $count++; $num++; } return $map; } private static function strings() { return array ( 6 => 'thousand', 9 => 'million', 12 => 'billion', 15 => 'trillion', 18 => 'quadrillion', 21 => 'quintillion', 24 => 'sextillion', 27 => 'septillion', 30 => 'octillion', 33 => 'nonillion', 36 => 'decillion', 39 => 'undecillion', 42 => 'duodecillion', 45 => 'tredecillion', 48 => 'quattuordecillion', 51 => 'quindecillion', 54 => 'sexdecillion', 57 => 'septendecillion', 60 => 'octodecillion', 63 => 'novemdecillion', 66 => 'vigintillion', 69 => 'unvigintillion', 72 => 'duovigintillion', 75 => 'trevigintillion', 78 => 'quattuorvigintillion', 81 => 'quinvigintillion', 84 => 'sexvigintillion', 87 => 'septenvigintillion', 90 => 'octovigintillion', 93 => 'novemvigintillion', 96 => 'trigintillion', 99 => 'untrigintillion', 102 => 'duotrigintillion', 105 => 'tretrigintillion', 108 => 'quattuortrigintillion', 111 => 'quintrigintillion', 114 => 'sextrigintillion', 117 => 'septentrigintillion', 120 => 'octotrigintillion', 123 => 'novemtrigintillion', 126 => 'quadragintillion', 129 => 'unquadragintillion', 132 => 'duoquadragintillion', 135 => 'trequadragintillion', 138 => 'quattuorquadragintillion', 141 => 'quinquadragintillion', 144 => 'sexquadragintillion', 147 => 'septenquadragintillion', 150 => 'octoquadragintillion', 153 => 'novemquadragintillion', 156 => 'quinquagintillion', 159 => 'unquinquagintillion', 162 => 'duoquinquagintillion', 165 => 'trequinquagintillion', 168 => 'quattuorquinquagintillion', 171 => 'quinquinquagintillion', 174 => 'sexquinquagintillion', 177 => 'septenquinquagintillion', 180 => 'octoquinquagintillion', 183 => 'novemquinquagintillion', 186 => 'sexagintillion', 189 => 'unsexagintillion', 192 => 'duosexagintillion', 195 => 'tresexagintillion', 198 => 'quattuorsexagintillion', 201 => 'quinsexagintillion', 204 => 'sexsexagintillion', 207 => 'septensexagintillion', 210 => 'octosexagintillion', 213 => 'novemsexagintillion', 216 => 'septuagintillion', 219 => 'unseptuagintillion', 222 => 'duoseptuagintillion', 225 => 'treseptuagintillion', 228 => 'quattuorseptuagintillion', 231 => 'quinseptuagintillion', 234 => 'sexseptuagintillion', 237 => 'septenseptuagintillion', 240 => 'octoseptuagintillion', 243 => 'novemseptuagintillion', 246 => 'octogintillion', 249 => 'unoctogintillion', 252 => 'duooctogintillion', 255 => 'treoctogintillion', 258 => 'quattuoroctogintillion', 261 => 'quinoctogintillion', 264 => 'sexoctogintillion', 267 => 'septenoctogintillion', 270 => 'octooctogintillion', 273 => 'novemoctogintillion', 276 => 'nonagintillion', 279 => 'unnonagintillion', 282 => 'duononagintillion', 285 => 'trenonagintillion', 288 => 'quattuornonagintillion', 291 => 'quinnonagintillion', 294 => 'sexnonagintillion', 297 => 'septennonagintillion', 300 => 'octononagintillion', 303 => 'novemnonagintillion', 306 => 'centillion', ); } public static function longform($number = string, $commas = true) { $negative = substr($number, 0, 1) == '-' ? 'negative ' : ''; list($number) = explode('.', $number); $number = trim(preg_replace("/[^0-9]/u","", $number)); $number = (string)(ltrim($number,'0')); if(empty($number)) return 'zero'; $length = strlen($number); if($length < 2) return $negative.self::ones($number); if($length < 3) return $negative.self::tens($number); if($length < 4) return $commas ? $negative.str_replace('hundred ', 'hundred and ', self::hundreds($number)) : $negative.self::hundreds($number); if($length < 307) { self::$map = self::map(); self::$strings = self::strings(); $result = self::beyond($number, self::$map[$length]); if(!$commas) return $negative.$result; $strings = self::$strings; $thousand = array_shift($strings); foreach($strings as $string) $result = str_replace($string.' ', $string.', ', $result); if(strpos($result, 'thousand') !== false) list($junk,$remainder) = explode('thousand', $result); else $remainder = $result; return strpos($remainder, 'hundred') !== false ? $negative.str_replace('thousand ', 'thousand, ', $result) : $negative.str_replace('thousand ', 'thousand and ', $result); } return 'a '.$negative.'number too big for your britches'; } private static function ones($number) { $ones = array('zero','one','two','three','four','five','six','seven','eight','nine'); return $ones[$number]; } private static function tens($number) { $number = (string)(ltrim($number,'0')); if(strlen($number) < 2) return self::ones($number); if($number < 20) { $teens = array('ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'); return $teens[($number-10)]; } else { $tens = array('','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'); $word = $tens[$number[0]]; return empty($number[1]) ? $word : $word.'-'.self::ones($number[1]); } } private static function hundreds($number) { $number = (string)(ltrim($number,'0')); if(strlen($number) < 3) return self::tens($number); $word = self::ones($number[0]).' hundred'; $remainder = substr($number, -2); if(ltrim($remainder,'0') != '') $word .= ' '.self::tens($remainder); return $word; } private static function beyond($number, $limit) { $number = (string)(ltrim($number,'0')); $length = strlen($number); if($length < 4) return self::hundreds($number); if($length < ($limit-2)) return self::beyond($number, self::$map[($limit-3)]); if($length == $limit) $word = self::hundreds(substr($number, 0, 3), true); elseif($length == ($limit-1)) $word = self::tens(substr($number, 0, 2)); else $word = self::ones($number[0]); $word .= ' '.self::$strings[$limit]; $sub = ($limit-3); $remainder = substr($number, -$sub); if(ltrim($remainder,'0') != '') $word .= ' '.self::beyond($remainder, self::$map[$sub]); return $word; } public static function numberformat($number, $fixed = 0, $dec = '.', $thou = ',') { $negative = substr($number, 0, 1) == '-' ? '-' : ''; $number = trim(preg_replace("/[^0-9\.]/u","", $number)); $number = (string)(ltrim($number,'0')); $fixed = (int)$fixed; if(!is_numeric($fixed)) $fixed = 0; if(strpos($number, $dec) !== false) list($number,$decimals) = explode($dec, $number); else $decimals = '0'; if($fixed) $decimals = '.'.str_pad(substr($decimals, 0, $fixed), $fixed, 0, STR_PAD_RIGHT); else $decimals = ''; $thousands = array_map('strrev', array_reverse(str_split(strrev($number), 3))); return $negative.implode($thou,$thousands).$decimals; } } |
1 | Amount in Words:<?=no_to_words($number)?> |
使用PHP函数将数字转换为单词的非常简单的方法。
我使用递归函数生成了这个。
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 | $wordnum = numberToWord($number); echo $wordnum.""; function singledigit($number){ switch($number){ case 0:$word ="zero";break; case 1:$word ="One";break; case 2:$word ="two";break; case 3:$word ="three";break; case 4:$word ="Four";break; case 5:$word ="Five";break; case 6:$word ="Six";break; case 7:$word ="Seven";break; case 8:$word ="Eight";break; case 9:$word ="Nine";break; } return $word; } function doubledigitnumber($number){ if($number == 0){ $word =""; } else{ $word = singledigit($number); } return $word; } function doubledigit($number){ switch($number[0]){ case 0:$word = doubledigitnumber($number[1]);break; case 1: switch($number[1]){ case 0:$word ="Ten";break; case 1:$word ="Eleven";break; case 2:$word ="Twelve";break; case 3:$word ="Thirteen";break; case 4:$word ="Fourteen";break; case 5:$word ="Fifteen";break; case 6:$word ="Sixteen";break; case 7:$word ="Seventeen";break; case 8:$word ="Eighteen";break; case 9:$word ="Ninteen";break; }break; case 2:$word ="Twenty".doubledigitnumber($number[1]);break; case 3:$word ="Thirty".doubledigitnumber($number[1]);break; case 4:$word ="Forty".doubledigitnumber($number[1]);break; case 5:$word ="Fifty".doubledigitnumber($number[1]);break; case 6:$word ="Sixty".doubledigitnumber($number[1]);break; case 7:$word ="Seventy".doubledigitnumber($number[1]);break; case 8:$word ="Eighty".doubledigitnumber($number[1]);break; case 9:$word ="Ninety".doubledigitnumber($number[1]);break; } return $word; } function unitdigit($numberlen,$number){ switch($numberlen){ case 3:$word ="Hundred";break; case 4:$word ="Thousand";break; case 5:$word ="Thousand";break; case 6:$word ="Lakh";break; case 7:$word ="Lakh";break; case 8:$word ="Crore";break; case 9:$word ="Crore";break; } return $word; } function numberToWord($number){ $numberlength = strlen($number); if ($numberlength == 1) { return singledigit($number); }elseif ($numberlength == 2) { return doubledigit($number); } else { $word =""; $wordin =""; if($numberlength == 9){ if($number[0] >0){ $unitdigit = unitdigit($numberlength,$number[0]); $word = doubledigit($number[0].$number[1]) ."".$unitdigit.""; return $word."".numberToWord(substr($number,2)); } else{ return $word."".numberToWord(substr($number,1)); } } if($numberlength == 7){ if($number[0] >0){ $unitdigit = unitdigit($numberlength,$number[0]); $word = doubledigit($number[0].$number[1]) ."".$unitdigit.""; return $word."".numberToWord(substr($number,2)); } else{ return $word."".numberToWord(substr($number,1)); } } if($numberlength == 5){ if($number[0] >0){ $unitdigit = unitdigit($numberlength,$number[0]); $word = doubledigit($number[0].$number[1]) ."".$unitdigit.""; return $word."".numberToWord(substr($number,2)); } else{ return $word."".numberToWord(substr($number,1)); } } else{ if($number[0] >0){ $unitdigit = unitdigit($numberlength,$number[0]); $word = singledigit($number[0]) ."".$unitdigit.""; } return $word."".numberToWord(substr($number,1)); } } } |