客户提出要求将? ?$1000 转为显示 ONE THOUSAND? DOLLARS?
/**
* 处理最大的数为9999亿,999billion, 12位
* @param string $money
*/
function toENMoney($money){
$rtnstr = "";
// 格式化为两位小数
$money = round($money, 2);
// 小于20的数组
$aa = ' ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,'
.' EIGHT, NINE, TEN, ELEVEN, TWELVE, THIRTEEN, FOURTEEN, FIFTEEN'
.' SIXTEEN, SEVENTEEN, EIGHTEEN, NINETEEN';
// 大于等于20的数组
$bb = " TWENTY, THIRTY, FORTY, FIFTY, SIXTY, SEVENTY, EIGHTY, NINETY";
$small = explode(',' , $aa);
$big = explode(',' , $bb);
$pointer_wz = strpos($money, '.');
// 取出整数部分
if ($pointer_wz > 0)
{
$integer = substr($money,0,$pointer_wz);
$pointer = substr($money,$pointer_wz+1);
}
else
$integer = $money;
// 格式化为12位数字
$integer = substr('000000000000', strlen($integer)).$integer;
if (strlen($pointer) == 1)
$pointer = $pointer.'0';
// 片段字符串
$littestr = "";
for ($i=0; $i<strlen($integer); $i=$i+3)
{
// 每次取三位数字
$littestr = substr($integer, $i, 3);
if ($littestr <= 0)
continue;
if ($littestr < 20)
{
if ($i == 9)
$rtnstr .= ' AND'.$small[$littestr-1];
else
$rtnstr .= $small[$littestr-1];
}else{
// 百位
$f = substr($littestr, 0, 1);
if ($f >0 )
$rtnstr .= $small[$f-1]. ' HUNDRED';
elseif ($i == 9)
$rtnstr .= $rtnstr==''?'':' AND';
$t1 = substr($littestr, 1, 1); // 十位
$t2 = substr($littestr, 2, 1); // 个位
// 十位和个位
$tt = substr($littestr, 1, 2);
if ($f>0 and $tt >0 and $i == 9)
$rtnstr .= $rtnstr==''?'':' AND';
if ($tt >0 and $tt < 20)
$rtnstr .= $small[$tt-1];
elseif ($tt > 20)
{
$rtnstr .= $big[$t1-2];
if ($t2 > 0)
$rtnstr .= '-'.ltrim($small[$t2-1]);
}
}
if ($i == 0)
$rtnstr .= ' BILLION';
elseif ($i == 3)
$rtnstr .= ' MILLION';
elseif ($i == 6)
$rtnstr .= ' THOUSAND';
}
// 处理小数部分
if ($pointer>0)
{
$rtnstr .= ' DOLLARS WITH';
if ($pointer < 20)
{
$rtnstr .= $small[$pointer-1];
}
else
{
$tt = substr($littestr, 1, 2);
$t1 = substr($pointer, 0, 1);
$t2 = substr($pointer, 1, 1);
$rtnstr .= $big[$t1-2];
if ($t2 > 0)
$rtnstr .= $small[$t2-1];
}
$rtnstr .= ' CENTS';
}
return $rtnstr;
}
|