9
A
回答
34
来源:http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
function convert_number_to_words($number) {
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
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 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number/10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number/100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number/$baseUnit);
$remainder = $number % $baseUnit;
$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}
用法示例:
echo convert_number_to_words(123456789);
// one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine
echo convert_number_to_words(123456789.123);
// one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine point one two three
echo convert_number_to_words(-1922685.477);
// negative one million, nine hundred and twenty-two thousand, six hundred and eighty-five point four seven seven
// float rounding can be avoided by passing the number as a string
echo convert_number_to_words(123456789123.12345); // rounds the fractional part
// one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two
echo convert_number_to_words('123456789123.12345'); // does not round
// one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two three four five
2
8
您还可以使用梨扩展为same.It具有多语言支持。
<?php
require('Numbers/Words.php');
$number = 637;
$nw = new Numbers_Words();
$numw = $nw->toWords($number);
print $numw; // prints six hundred thirty-seven
?>
toWord()
也需要它打印 soixante-dix-huit
(78法语)可选区域设置特定的参数,如toWord(78,'fr')
1
相关问题
- 1. 有没有简单的方法将数字转换为PHP中的单词?
- 2. 有没有办法将Greasemonkey脚本自动转换为书签?
- 3. 有没有办法将数字转换为java中的复数?
- 4. shell脚本 - 有没有办法将数字转换为char?
- 5. 有没有简单的方法将C#类转换为PHP?
- 6. 有没有简单的方法将值转换为字符串?
- 7. 有没有办法在PHP中将GUID转换为UUID?
- 8. 将Matlab/C函数转换为没有源代码的版本
- 9. 有没有办法在postgresql中将文本转换为时间?
- 10. 有没有一种简单的方法在JavaScript中将文本转换为HTML?
- 11. 有没有简单的方法将gedit插件从gnome2版本转换为gnome3版本?
- 12. scala中有没有将任何Map转换为java.util.Map的方法?
- 13. 有没有任何PHP函数将文本数字转换为数字?例如:转换( “九”)输出9
- 14. 有没有一种有效的方法将Bundle转换为ContentValues?
- 15. 有没有办法将decltype转换为宏中的字符串?
- 16. 有没有一种方法可以在ActionScript中将文本转换为圆形?
- 17. 有没有办法将查询字符串转换为数组?
- 18. 转换字节数组为整数没有循环的方法?
- 19. 有没有更好的方法将字节数组转换为int?
- 20. 如何在PHP中将PDF版本1.5转换为版本1.4
- 21. 将所有本地化字符串转换为JSON的方法?
- 22. 有没有在Visual Studio中将文本转换为C#字符串文字的方法?
- 23. 有没有简单的方法将标准转换为HQL?
- 24. 有没有将Collection转换为F#序列的安全方法?
- 25. 有没有“正确的”方法将django.db.models.query.ValuesListQuerySet转换为纯List?
- 26. 有没有办法将SQL Server 2008 mdf文件转换为SQL Server Express版本?
- 27. 在scikit-image中将轮廓转换为numpy数组蒙版的有效方法?
- 28. 有没有一种简单的方法在开发时切换PHP版本?
- 29. 有没有办法在java中将字节转换为布尔数组?
- 30. 有没有办法将Trac Wiki页面转换为HTML?
在哪一种语言,英语? – m4t1t0
是的,英文 – Sree
@ user1441638 - http://stackoverflow.com/a/14315090/922198 –