PHP 金额数字转换成英文
程序员文章站
2024-01-27 08:18:58
复制代码 代码如下:
<?php
$num=1220.01;
echo fmoney($num);//结果:1,220.21
echo umoney($num);
//结果:one thousand and two hundred twenty dollars and twenty-one cents only
echo umoney($num,"rmb");
//结果:one thousand and two hundred twenty yuan and twenty-one fen only
//define
//格式化货币
function fmoney($num) {
$num=0+$num;
$num = sprintf("%.02f",$num);
if(strlen($num) <= 6) return $num;
//从最后开始算起,每3个数它加一个","
for($i=strlen($num)-1,$k=1, $j=100; $i >= 0; $i--,$k++) {
$one_num = substr($num,$i,1);
if($one_num ==".") {
$numarray[$j--] = $one_num;
$k=0;
continue;
}
if($k%3==0 and $i!=0) {
//如果正好只剩下3个数字,则不加','
$numarray[$j--] = $one_num;
$numarray[$j--] = ",";
$k=0;
} else {
$numarray[$j--]=$one_num;
}
}
ksort($numarray);
return join("",$numarray);
}
function umoney($num,$type="usd") {
global $numtable,$commatable,$moneytype;
//global $numtable;
$numtable[0]="zero ";
$numtable[1]="one ";
$numtable[2]="two ";
$numtable[3]="three ";
$numtable[4]="four ";
$numtable[5]="five ";
$numtable[6]="six ";
$numtable[7]="seven ";
$numtable[8]="eight ";
$numtable[9]="nine ";
$numtable[10]="ten ";
$numtable[11]="eleven ";
$numtable[12]="twelve ";
$numtable[13]="thirteen ";
$numtable[14]="fourteen ";
$numtable[15]="fifteen ";
$numtable[16]="sixteen ";
$numtable[17]="seventeen ";
$numtable[18]="eighteen ";
$numtable[19]="nineteen ";
$numtable[20]="twenty ";
$numtable[30]="thirty ";
$numtable[40]="forty ";
$numtable[50]="fifty ";
$numtable[60]="sixty ";
$numtable[70]="seventy ";
$numtable[80]="eighty ";
$numtable[90]="ninety ";
$commatable[0]="hundred ";
$commatable[1]="thousand ";
$commatable[2]="million ";
$commatable[3]="milliard ";
$commatable[4]="billion ";
$commatable[5]="????? ";
//单位
$moneytype["usd"]="dollars ";
$moneytype["usd_1"]="cents only";
$moneytype["rmb"]="yuan ";
$moneytype["rmb_1"]="fen only";
if($type=="") $type="usd";
$fnum = fmoney($num);
$numarray = explode(",",$fnum);
$resultarray = array();
$k=0;
$cc=count($numarray);
for($i = 0; $i < count($numarray); $i++) {
$num_str = $numarray[$i];
//echo "<br>";
//小数位的处理400.21
if(eregi("\.",$num_str)) {
$dotarray = explode(".",$num_str);
if($dotarray[1] != 0) {
$resultarray[$k++]=format3num($dotarray[0]+0);
$resultarray[$k++]=$moneytype[strtolower($type)];
$resultarray[$k++]="and ";
$resultarray[$k++]=format3num($dotarray[1]+0);
$resultarray[$k++]=$moneytype[strtolower($type)."_1"];
} else {
$resultarray[$k++]=format3num($dotarray[0]+0);
$resultarray[$k++]=$moneytype[strtolower($type)];
}
} else {
//非小数位的处理
if(($num_str+0)!=0) {
$resultarray[$k++]=format3num($num_str+0);
$resultarray[$k++]=$commatable[--$cc];
//判断:除小数外其余若不为零则加and
for($j=$i; $j <= $cc; $j++) {
//echo "<br>";
//echo $numarray[$j];
if($numarray[$j] !=0) {
$resultarray[$k++]="and ";
break;
}
}
}
}
}
return join("",$resultarray);
}
function format3num($num) {
global $numtable,$commatable;
$numlen = strlen($num);
for($i = 0,$j = 0;$i < $numlen; $i++) {
$bitenum[$j++] = substr($num,$i,1);
}
if($num==0) return "";
if($numlen == 1) return $numtable[$num];
if($numlen == 2) {
if($num <= 20) return $numtable[$num];
//第一位不可能零
if($bitenum[1]==0) {
return $numtable[$num];
} else {
return trim($numtable[$bitenum[0]*10])."-".$numtable[$bitenum[1]];
}
}
//第一个不可能为零
if($numlen == 3) {
if($bitenum[1]==0 && $bitenum[2]==0) {
//100
return $numtable[$bitenum[0]].$commatable[0];
} elseif($bitenum[1]==0) {
//102
return $numtable[$bitenum[0]].$commatable[0].$numtable[$bitenum[2]];
} elseif ($bitenum[2]==0) {
//120
return $numtable[$bitenum[0]].$commatable[0].$numtable[$bitenum[1]*10];
} else {
//123
return $numtable[$bitenum[0]].$commatable[0].trim($numtable[$bitenum[1]*10])."-".$numtable[$bitenum[2]];
}
}
return $num;
}
?>
复制代码 代码如下:
<?php
$num=1220.01;
echo fmoney($num);//结果:1,220.21
echo umoney($num);
//结果:one thousand and two hundred twenty dollars and twenty-one cents only
echo umoney($num,"rmb");
//结果:one thousand and two hundred twenty yuan and twenty-one fen only
//define
//格式化货币
function fmoney($num) {
$num=0+$num;
$num = sprintf("%.02f",$num);
if(strlen($num) <= 6) return $num;
//从最后开始算起,每3个数它加一个","
for($i=strlen($num)-1,$k=1, $j=100; $i >= 0; $i--,$k++) {
$one_num = substr($num,$i,1);
if($one_num ==".") {
$numarray[$j--] = $one_num;
$k=0;
continue;
}
if($k%3==0 and $i!=0) {
//如果正好只剩下3个数字,则不加','
$numarray[$j--] = $one_num;
$numarray[$j--] = ",";
$k=0;
} else {
$numarray[$j--]=$one_num;
}
}
ksort($numarray);
return join("",$numarray);
}
function umoney($num,$type="usd") {
global $numtable,$commatable,$moneytype;
//global $numtable;
$numtable[0]="zero ";
$numtable[1]="one ";
$numtable[2]="two ";
$numtable[3]="three ";
$numtable[4]="four ";
$numtable[5]="five ";
$numtable[6]="six ";
$numtable[7]="seven ";
$numtable[8]="eight ";
$numtable[9]="nine ";
$numtable[10]="ten ";
$numtable[11]="eleven ";
$numtable[12]="twelve ";
$numtable[13]="thirteen ";
$numtable[14]="fourteen ";
$numtable[15]="fifteen ";
$numtable[16]="sixteen ";
$numtable[17]="seventeen ";
$numtable[18]="eighteen ";
$numtable[19]="nineteen ";
$numtable[20]="twenty ";
$numtable[30]="thirty ";
$numtable[40]="forty ";
$numtable[50]="fifty ";
$numtable[60]="sixty ";
$numtable[70]="seventy ";
$numtable[80]="eighty ";
$numtable[90]="ninety ";
$commatable[0]="hundred ";
$commatable[1]="thousand ";
$commatable[2]="million ";
$commatable[3]="milliard ";
$commatable[4]="billion ";
$commatable[5]="????? ";
//单位
$moneytype["usd"]="dollars ";
$moneytype["usd_1"]="cents only";
$moneytype["rmb"]="yuan ";
$moneytype["rmb_1"]="fen only";
if($type=="") $type="usd";
$fnum = fmoney($num);
$numarray = explode(",",$fnum);
$resultarray = array();
$k=0;
$cc=count($numarray);
for($i = 0; $i < count($numarray); $i++) {
$num_str = $numarray[$i];
//echo "<br>";
//小数位的处理400.21
if(eregi("\.",$num_str)) {
$dotarray = explode(".",$num_str);
if($dotarray[1] != 0) {
$resultarray[$k++]=format3num($dotarray[0]+0);
$resultarray[$k++]=$moneytype[strtolower($type)];
$resultarray[$k++]="and ";
$resultarray[$k++]=format3num($dotarray[1]+0);
$resultarray[$k++]=$moneytype[strtolower($type)."_1"];
} else {
$resultarray[$k++]=format3num($dotarray[0]+0);
$resultarray[$k++]=$moneytype[strtolower($type)];
}
} else {
//非小数位的处理
if(($num_str+0)!=0) {
$resultarray[$k++]=format3num($num_str+0);
$resultarray[$k++]=$commatable[--$cc];
//判断:除小数外其余若不为零则加and
for($j=$i; $j <= $cc; $j++) {
//echo "<br>";
//echo $numarray[$j];
if($numarray[$j] !=0) {
$resultarray[$k++]="and ";
break;
}
}
}
}
}
return join("",$resultarray);
}
function format3num($num) {
global $numtable,$commatable;
$numlen = strlen($num);
for($i = 0,$j = 0;$i < $numlen; $i++) {
$bitenum[$j++] = substr($num,$i,1);
}
if($num==0) return "";
if($numlen == 1) return $numtable[$num];
if($numlen == 2) {
if($num <= 20) return $numtable[$num];
//第一位不可能零
if($bitenum[1]==0) {
return $numtable[$num];
} else {
return trim($numtable[$bitenum[0]*10])."-".$numtable[$bitenum[1]];
}
}
//第一个不可能为零
if($numlen == 3) {
if($bitenum[1]==0 && $bitenum[2]==0) {
//100
return $numtable[$bitenum[0]].$commatable[0];
} elseif($bitenum[1]==0) {
//102
return $numtable[$bitenum[0]].$commatable[0].$numtable[$bitenum[2]];
} elseif ($bitenum[2]==0) {
//120
return $numtable[$bitenum[0]].$commatable[0].$numtable[$bitenum[1]*10];
} else {
//123
return $numtable[$bitenum[0]].$commatable[0].trim($numtable[$bitenum[1]*10])."-".$numtable[$bitenum[2]];
}
}
return $num;
}
?>