我遇到了一个大问题解决这个算法在PHP的想法是当:
1.用户输入为〈1000(999)输出应为九百九十九毫米
1.当用户输入〉999时,输出应为1第纳尔,因为第纳尔等于1000毫米,例如:如果输入为1,590,则输出= 1第纳尔和590毫米另一个示例,如果输入为275,590,则输出= 275第纳尔和590毫米我根据一些文章编写了此代码,但出现了一个大错误
<?php function numtowords($num){
$decones = array(
'01' => "One",
'02' => "Two",
'03' => "Three",
'04' => "Four",
'05' => "Five",
'06' => "Six",
'07' => "Seven",
'08' => "Eight",
'09' => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen"
);
$ones = array(
0 => " ",
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"
);
$tens = array(
0 => "",
2 => "Twenty",
3 => "Thirty",
4 => "Forty",
5 => "Fifty",
6 => "Sixty",
7 => "Seventy",
8 => "Eighty",
9 => "Ninety"
);
$hundreds = array(
"Hundred",
"Thousand",
"Million",
"Billion",
"Trillion",
"Quadrillion"
); //limit t quadrillion
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr);
$rettxt = "";
foreach($whole_arr as $key => $i){
if($i < 20){
$rettxt .= $ones[$i];
}
elseif($i < 100){
$rettxt .= $tens[substr($i,0,1)];
$rettxt .= " ".$ones[substr($i,1,1)];
}
else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= " ".$hundreds[$key]." ";
}
}
$rettxt = $rettxt." dinar/s";
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $decones[$decnum];
}
elseif($decnum < 1000){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
$rettxt = $rettxt." FRANK/s";
}
return $rettxt;}
echo numtowords(275,590);
?>
这里输出是二百七十五第纳尔/秒而不是二百七十五第纳尔五百九十毫米
1条答案
按热度按时间von4xj4u1#
当你在函数调用中使用逗号“,”时,它被认为是一个参数分隔符,所以对于函数numtwords(),你传递的是两个参数,而不是一个
275
&590
:所以它只考虑275,忽略第二个参数,因为函数声明只接收一个参数**$num**。
调用必须使用点
.
: