您的位置:首页 > 编程语言 > PHP开发

PHP 转换人民币数字为大写

2020-06-29 04:43 176 查看
echo RMB_Transform(0.0121);
exit("\n");
/**
  *函数名称: RMB_Transform		作者: Dandy.Mu
  *谱写日期: 忘记了
  *函数说明: 将指定的金额转换为中文大写模式
  *参数说明: 
* @Money => 金额[数字型]
  *返回信息: 转换后的大写字符
  */
function RMB_Transform($Money)
{
if ( ! is_numeric($Money) ) return false;

$Number  = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');	// 数字翻译
$lMarked = array('拾','佰','仟');										// 小数点左边数字的标示
$rMarked = array('角','分','哩','毫');									// 小数点右边数字的标示
$bMarKed = array('圆', '万', '亿');										// 每组尾数的标识
list($Left, $Right) = explode('.', $Money);								// 分割小数点的左右
while (strlen($Left) >= 1) {											// 将数字分割为4个一组
$aLeft[] = substr($Left, -4, 4);	$Left = substr($Left, 0, -4);
}
krsort($aLeft);															// 逆向排序数组,因为要先翻译最大的数
foreach ($aLeft as $Key1 => $Value1) {									// 循环上面的分组后的字符
$tLeft = str_split($Value1);	$ctLeft = count($tLeft) - 2;		// 将分组后的字符分割并计算数组的长度

# ------- Head ------------------------- #
# 循环分割为单个字符的数组,然后进行翻译。#
# 如果值为0的话,则只赋值并不赋予标识。  #
# ------- End -------------------------- #
foreach ($tLeft as $Key2 => $Value2)
$tretData .= $Value2 ? $Number[$Value2].$lMarked[$ctLeft-$Key2] : $Number[0];

$tretData = preg_replace('/('.$Number[0].')+$/', '', $tretData);					// 去除尾部的 零
$retData .= $tretData.(empty($tretData) ? '' : $bMarKed[$Key1 > 2 ? 1 : $Key1]);	// 给最终结果赋值,并增加特殊位标识也就是 圆 万 亿
unset($Key2, $Value2, $tretData);
}
unset($tLeft, $ctLeft, $aLeft, $Left, $Key1, $Value1);
// 如果有小数点后面的数字的话,则进行翻译。
if ( is_numeric($Right) ) {
foreach (str_split(substr($Right, 0, 4)) as $Key3 => $Value3) {
$tretData .= $Value3 ? $Number[$Value3].$rMarked[$Key3] : $Number[0];
}
$tretData = preg_replace('/^('.$Number[0].')+/', '', $tretData);					// 去除尾部的 零
}
$retData .= preg_replace('/('.$Number[0].')+/', $Number[0], $tretData);					// 将多个的 零 替换为一个 零
unset($Right, $Key3, $Value3, $tretData, $Number, $lMarked, $rMarked, $bMarKed);
return $retData;
}
[/code]

转载于:https://my.oschina.net/CocoFather/blog/318484

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: