数字转英文
程序员文章站
2022-04-22 15:25:22
<?php //___{xf_num2en}________________________________________
//*** 說明: 數值轉英文表示法
//=== 回傳: <string>
//--- nn)數值 ff)小數位
//============================================================
function xf_num2en($nn, $ff=0) {
//===[前置]========================================
if (!is_numeric($nn)) return '';
($ff>2) and $ff=2;
$xn=''; $xf='';
global $enws;
$enws=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=>"forty",50=>"fifty",
60=>"sixty",70=>"seventy",80=>"eighty",90=>"ninety");
//===[整數]========================================
$nk=floor($nn);
$cnt=0;
while ($nk) {
$n=$nk % 1000;
if ($n) {
$x=xf_ennum4($n);
if ($cnt==1) $xn=$x. 'thousand '. $xn;
elseif ($cnt==2) $xn=$x. 'million '. $xn;
elseif ($cnt==3) $xn=$x. 'billion '. $xn;
elseif ($cnt==4) $xn=$x. 'trillion '. $xn;
else $xn=$x;
}
$cnt+=1;
$nk=floor($nk/1000);
} //--while
//===[小數]========================================
if ($ff>0) {
$n=floor($nn*100) % 100;
($n) and $xf=xf_ennum4($n). 'cent';
}
return $xn.$xf;
} //--xf_num2en
function xf_ennum4($nn) {
global $enws;
$ans='';
$n=floor($nn/100);
($n) and $ans=$enws[$n]. ' hundred ';
$n=$nn % 100;
if ($n) {
if ($n<20) $ans.=$enws[$n]. ' ';
else {
$m=floor($n/10) * 10;
$ans.=$enws[$m]. ' ';
$n=$n % 10;
($n) and $ans.=$enws[$n]. ' ';
}
}
return $ans;
} //--xf_ennum4 ?>
//*** 說明: 數值轉英文表示法
//=== 回傳: <string>
//--- nn)數值 ff)小數位
//============================================================
function xf_num2en($nn, $ff=0) {
//===[前置]========================================
if (!is_numeric($nn)) return '';
($ff>2) and $ff=2;
$xn=''; $xf='';
global $enws;
$enws=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=>"forty",50=>"fifty",
60=>"sixty",70=>"seventy",80=>"eighty",90=>"ninety");
//===[整數]========================================
$nk=floor($nn);
$cnt=0;
while ($nk) {
$n=$nk % 1000;
if ($n) {
$x=xf_ennum4($n);
if ($cnt==1) $xn=$x. 'thousand '. $xn;
elseif ($cnt==2) $xn=$x. 'million '. $xn;
elseif ($cnt==3) $xn=$x. 'billion '. $xn;
elseif ($cnt==4) $xn=$x. 'trillion '. $xn;
else $xn=$x;
}
$cnt+=1;
$nk=floor($nk/1000);
} //--while
//===[小數]========================================
if ($ff>0) {
$n=floor($nn*100) % 100;
($n) and $xf=xf_ennum4($n). 'cent';
}
return $xn.$xf;
} //--xf_num2en
function xf_ennum4($nn) {
global $enws;
$ans='';
$n=floor($nn/100);
($n) and $ans=$enws[$n]. ' hundred ';
$n=$nn % 100;
if ($n) {
if ($n<20) $ans.=$enws[$n]. ' ';
else {
$m=floor($n/10) * 10;
$ans.=$enws[$m]. ' ';
$n=$n % 10;
($n) and $ans.=$enws[$n]. ' ';
}
}
return $ans;
} //--xf_ennum4 ?>
推荐阅读