在PHP中,有一种简单的方法可以将数字转换为单词吗?举例来说,27到27.
我在网上发现了一些(2007/2008)源代码,因为它是版权但我可以随意使用它并按我的意愿修改它,所以我把它放在这里并在CC-Wiki下重新许可:
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"; } }
或者,您可以使用intl
PHP中的包中的NumberFormatter类.这是一个示例代码,可以帮助您入门(对于命令行):
format($num).' is '.$nf2->format($num)."\n"; }
PECL 有Numbers_Words
包装.它完全符合您的要求.支持以下语言:
bg(保加利亚语)由Kouber Saparev提供
cs(捷克语)由Petr'PePa'Pavel
de(德国)Piotr Klaban
dk(丹麦语)由Jesper Veggerby执导
由Piotr Klaban创作的en_100(Donald Knuth系统,英文)
由Piotr Klaban创作的en_GB(英式英语)
由Piotr Klaban创作的en_US(美国英语)
西雅图(西班牙卡斯特拉诺)的Xavier Noguer
es_AR(阿根廷西班牙语)由Martin Marrese撰写
et(爱沙尼亚语),Erkki Saarniit
fr(法语)由Kouber Saparev撰写
fr_BE(法国比利时)由Kouber Saparev和Philippe Bajoit创作
他(希伯来语)由哈达尔波拉特
hu_HU(匈牙利语),Nils Homp
id(印度尼西亚语)由Ernas M. Jamil和Arif Rifai Dwiyanto创作
it_IT(意大利语)由Filippo Beltramini和Davide Caironi完成
LT(立陶宛语)由Laurynas Butkus提供
nl(荷兰语)由WHAM van Dinter提供
pl(波兰语)由Piotr Klaban提供
pt_BR(巴西葡萄牙语)由Marcelo Subtil Marcal和Mario HCT撰写
ru(俄罗斯)作者:Andrey Demenev
sv(瑞典语),罗宾爱立信