php 数字转人民币,php数字转人民币金额大写
數值轉人民幣大寫/**
*?數字轉人民幣金額大寫
*?Class?Ext_Num2Cny
*/
class?Number2Cny
{
static?$basic?=?array(0?=>?"零",?"壹",?"貳",?"叁",?"肆",?"伍",?"陸",?"柒",?"捌",?"玖");
static?$advanced?=?array(1?=>?"拾",?"佰",?"仟");
public?static?function?parseNumber($number)
{
$number?=?trim($number);
if?($number?>?999999999999)?{
return?"無法處理";
}
if?($number?==?0)?{
return?"零";
}
if?(strpos($number,?'.'))?{
$number?=?round($number,?2);
$data?=?explode(".",?$number);
$data[0]?=?self::parseInt($data[0]);
$data[1]?=?self::parseDec($data[1]);
return?$data[0]?.?$data[1];
}?else?{
return?self::parseInt($number)?.?'整';
}
}
/**
*?整數部分
*?@param?$number
*?@return?string
*/
public?static?function?parseInt($number)
{
$arr?=?array_reverse(str_split($number));
$data?=?'';
$zero_num?=?0;
foreach?($arr?as?$k?=>?$v)?{
$_chinese?=?'';
$zero?=?($v?==?0)???true?:?false;
$x?=?$k?%?4;
if?($x?&&?$zero?&&?$zero_num?>?1)?{
continue;
}
switch?($x)?{
case?0:
if?($zero)?{
$zero_num?=?0;
}?else?{
$_chinese?=?self::$basic[$v];
$zero_num?=?1;
}
if?($k?==?8)?{
$_chinese?.=?'億';
}?elseif
($k?==?4)?{
$_chinese?.=?'萬';
}
break;
default:
if?($zero)?{
if?($zero_num?==?1)?{
$_chinese?=?self::$basic[$v];
$zero_num++;
}
}?else?{
$_chinese?=?self::$basic[$v];
$_chinese?.=?self::$advanced[$x];
}
}
$data?=?$_chinese?.?$data;
}
if?(empty($data))?{
$data?=?self::$basic[0];
}
return?$data?.?'元';
}
/**
*?小數部分
*?@param?$number
*?@return?string
*/
public?static?function?parseDec($number)
{
if?(strlen($number)?
$arr?=?array_reverse(str_split($number));
$data?=?'';
$zero_num?=?false;
foreach?($arr?as?$k?=>?$v)?{
$zero?=?($v?==?0)???true?:?false;
$_chinese?=?'';
if?($k?==?0)?{
if?(!$zero)?{
$_chinese?=?self::$basic[$v];
$_chinese?.=?'分';
$zero_num?=?true;
}
}?else?{
if?($zero)?{
if?($zero_num)?{
$_chinese?=?self::$basic[$v];
}
}?else?{
$_chinese?=?self::$basic[$v];
$_chinese?.=?'角';
}
}
$data?=?$_chinese?.?$data;
}
return?$data;
}
}
使用echo?Number2Cny::parseNumber(123456);//壹拾貳萬叁仟肆佰伍拾陸元整
echo?"\n";
echo?Number2Cny::parseNumber(0.12);//元壹角貳分
echo?"\n";
echo?Number2Cny::parseNumber(10);//元壹角貳分
echo?"\n";
echo?Number2Cny::parseNumber(123456.12);//壹拾貳萬叁仟肆佰伍拾陸元壹角貳分
echo?"\n";
echo?Number2Cny::parseNumber(123456789012.1234);?//壹仟貳佰叁拾肆億伍仟陸佰柒拾捌萬玖仟零壹拾貳元壹角貳分
總結
以上是生活随笔為你收集整理的php 数字转人民币,php数字转人民币金额大写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: q5机顶盒刷linux系统,数码视讯Q5
- 下一篇: 人才数据报告不会写?指标不明晰?这套人力