<?
function num2string($num)
{
$digit=Array("หนึ่ง","สอง","สาม","สี่","ห้า","หก","เจ็ด","แปด","เก้า");
$unit=Array("สิบ","ร้อย","พัน","หมื่น","แสน");
if($num==0)
return "ศูนย์บาทถ้วน";
if(strpos($num,".")==0)
$num.=".00";
$tmp=substr($num,0,strpos($num,"."));
while(strlen($tmp)>6)
{
$cut=strlen($tmp)%6;
if($cut==0)$cut=6;
$data=substr($tmp,0,$cut);
for($i=0;$i<strlen($data)-2;$i++)
{
if($data[$i]==0)
continue;
$ans.=$digit[$data[$i]-1].$unit[strlen($data)-$i-2];
}
$ans.=num2string_2digit(substr($data,strlen($data)-2))."ล้าน";
$tmp=substr($tmp,$cut);
}
for($i=0;$i<strlen($tmp)-2;$i++)
{
if($tmp[$i]==0)
continue;
$ans.=$digit[$tmp[$i]-1].$unit[strlen($tmp)-$i-2];
}
$ans.=num2string_2digit(substr($tmp,strlen($tmp)-2))."บาท";
$tmp=substr($num,strpos($num,".")+1);
if(substr($tmp,0,2)=="00")
return $ans."ถ้วน";
return $ans.num2string_2digit($tmp)."สตางค์";
}
function num2string_2digit($num)
{
$digit=Array("ศูนย์","หนึ่ง","สอง","สาม","สี่","ห้า","หก","เจ็ด","แปด","เก้า");
$ans="";
$num=sprintf("%d",$num);
if(strlen($num)==1)
return $digit[$num];
if($num[0]==2)
$ans.="ยี่";
else if($num[0]>2)
$ans.=$digit[$num[0]];
if($num[0]>0)
$ans.="สิบ";
if($num[1]>1)
$ans.=$digit[$num[1]];
else if($num[1]==1)
$ans.="เอ็ด";
return $ans;
}
echo num2string("8821.25");
?>
function num2string($num)
{
$digit=Array("หนึ่ง","สอง","สาม","สี่","ห้า","หก","เจ็ด","แปด","เก้า");
$unit=Array("สิบ","ร้อย","พัน","หมื่น","แสน");
if($num==0)
return "ศูนย์บาทถ้วน";
if(strpos($num,".")==0)
$num.=".00";
$tmp=substr($num,0,strpos($num,"."));
while(strlen($tmp)>6)
{
$cut=strlen($tmp)%6;
if($cut==0)$cut=6;
$data=substr($tmp,0,$cut);
for($i=0;$i<strlen($data)-2;$i++)
{
if($data[$i]==0)
continue;
$ans.=$digit[$data[$i]-1].$unit[strlen($data)-$i-2];
}
$ans.=num2string_2digit(substr($data,strlen($data)-2))."ล้าน";
$tmp=substr($tmp,$cut);
}
for($i=0;$i<strlen($tmp)-2;$i++)
{
if($tmp[$i]==0)
continue;
$ans.=$digit[$tmp[$i]-1].$unit[strlen($tmp)-$i-2];
}
$ans.=num2string_2digit(substr($tmp,strlen($tmp)-2))."บาท";
$tmp=substr($num,strpos($num,".")+1);
if(substr($tmp,0,2)=="00")
return $ans."ถ้วน";
return $ans.num2string_2digit($tmp)."สตางค์";
}
function num2string_2digit($num)
{
$digit=Array("ศูนย์","หนึ่ง","สอง","สาม","สี่","ห้า","หก","เจ็ด","แปด","เก้า");
$ans="";
$num=sprintf("%d",$num);
if(strlen($num)==1)
return $digit[$num];
if($num[0]==2)
$ans.="ยี่";
else if($num[0]>2)
$ans.=$digit[$num[0]];
if($num[0]>0)
$ans.="สิบ";
if($num[1]>1)
$ans.=$digit[$num[1]];
else if($num[1]==1)
$ans.="เอ็ด";
return $ans;
}
echo num2string("8821.25");
?>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น