我正在开发一个简单的Web应用程序,我需要以字符串格式向用户显示数字a。
示例:
12 - One Two or Twelve
-20 - minus Two zero or minus twenty
无论哪种方式都没问题。我需要在PHP中完成此操作。任何帮助将不胜感激。
答案 0 :(得分:7)
第一个选项(拼出数字),strtr是你的朋友
$words = array(
'-' => 'minus ',
'1' => 'one ',
'2' => 'two ',
etc....
);
echo strtr(-123, $words);
答案 1 :(得分:3)
如果您想拼出完整号码,可以使用PEAR
Numbers_Words class。此类有一个toWords
()方法,它接受+ve
或-ve
num并返回该数字的拼写字符串表示形式。
如果您想将数字转换为字符串数字,我不知道任何lib函数。但是你可以轻松地自己编码。 user187291 gives a good way to do this in his answer
<?php
$arr = array(
-12,
20
);
foreach($arr as $num) {
$nw = new Numbers_Words();
echo "$num = ". $nw->toWords($num)."\n";
}
?>
输出:
C:\>php a.php
-12 = minus twelve
20 = twenty
答案 2 :(得分:1)
function convertToString($number, $blankIfZero=true){
$strRep = "";
$n = intval($number);
$one2twenty = array("One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen");
$twenty2ninty = array("Twenty", "Thirty",
"Fourty", "Fifty", "Sixty", "Seventy", "Eighty",
"Ninety");
$hundred = "Hundred";
$thousand = "Thousand";
$million = "Million";
$billion = "Billion";
switch($n){
case 0:
if($blankIfZero == true){
$strRep= $strRep."";
break;
}else{
$strRep = $strRep."Zero";
break;
}
case $n >0 && $n <20:
$strRep = $strRep." ".$one2twenty[($n-1)];
break;
case $n >=20 && $n < 100:
$strRep = $strRep . " ". $twenty2ninty[(($n/10) - 2)];
$strRep .= convertToString($n%10);
break;
case $n >= 100 && $n <= 999:
$strRep = $strRep.$one2twenty[(($n/100)-1)]." ".$hundred. " ";
$strRep .= convertToString($n%100);
break;
case $n >= 1000 && $n < 100000:
if($n < 20000){
$strRep = $strRep.$one2twenty[(($n/1000)-1)]." ".$thousand. " ";
$strRep .= convertToString($n%1000);
break;
}else{
$strRep = $strRep . $twenty2ninty[(($n/10000) - 2)];
$strRep .= convertToString($n%10000);
break;
}
case $n >= 100000 && $n < 1000000:
$strRep .= convertToString($n/1000). " ".$thousand. " ";
$strRep .= convertToString(($n%100000)%1000);
break;
case $n >= 1000000 && $n < 10000000:
$strRep = $strRep . $one2twenty[(($n/1000000) - 1)]. " ".$million." ";
$strRep .= convertToString($n%1000000);
break;
case $n >= 10000000 && $n < 10000000000:
$strRep .= convertToString($n/1000000). " ".$million. " ";
$strRep .= convertToString(($n%1000000));
break;
}
return $strRep;
}
答案 3 :(得分:1)
在@SRC在之前的回答中所做的工作的基础上,我编写了一个函数来将任何原始无符号整数转换为英语,主要是出于好奇。
您可以在此处找到代码:a GitHub Gist by StampyCode
此代码仅受PHP的整数限制(64位)限制:
9223372036854775807
此代码处理的是输出:
九百万,二百二十三千万亿,三百七十二万亿,三十六十亿,八百五十四百万,七十七万五千八百七十七
代码嵌入:
<?php
$_1to19 = [
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
];
$_teen = [
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
];
$_mult = [
2 => 'hundred',
3 => 'thousand',
6 => 'million',
9 => 'billion',
12 => 'trillion',
15 => 'quadrillion',
18 => 'quintillion',
21 => 'sextillion',
24 => 'septillion', // php can't count this high
27 => 'octillion',
];
$fnBase = function ($n, $x) use (&$fn, $_mult) {
return $fn($n / (10 ** $x)) . ' ' . $_mult[$x];
};
$fnOne = function ($n, $x) use (&$fn, &$fnBase) {
$y = ($n % (10 ** $x)) % (10 ** $x);
$s = $fn($y);
$sep = ($x === 2 && $s ? " and " : ($y < 100 ? ($y ? " and " : '') : ', '));
return $fnBase($n, $x) . $sep . $s;
};
$fnHundred = function ($n, $x) use (&$fn, &$fnBase) {
$y = $n % (10 ** $x);
$sep = ($y < 100 ? ($y ? ' and ' : '') : ', ');
return ', ' . $fnBase($n, $x) . $sep . $fn($y);
};
$fn = function ($n) use (&$fn, $_1to19, $_teen, $number, &$fnOne, &$fnHundred) {
switch ($n) {
case 0:
return ($number > 1 ? '' : 'zero');
case $n < 20:
return $_1to19[$n - 1];
case $n < 100:
return $_teen[($n / 10) - 2] . ' ' . $fn($n % 10);
case $n < (10 ** 3):
return $fnOne($n, 2);
};
for ($i = 4; $i < 27; ++$i) {
if ($n < (10 ** $i)) {
break;
}
}
return ($i % 3) ? $fnHundred($n, $i - ($i % 3)) : $fnOne($n, $i - 3);
};
$number = $fn((int)$number);
$number = str_replace(', , ', ', ', $number);
$number = str_replace(', ', ', ', $number);
$number = str_replace(' ', ' ', $number);
$number = ltrim($number, ', ');
return $number;