加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 数值金额转换为中文大写金额

发布时间:2020-12-13 21:32:29 所属栏目:PHP教程 来源:网络整理
导读:/** * 将数值金额转换为中文大写金额 * @param $amount float 金额(分) * @param $type int 补整类型,0:到角补整;1:到元补整 * @return mixed 中文大写金额 */ function convertAmountToCn($amount,$type = 1) { if ($amount == 0) { return "零元整"; } ? i
/**
* 将数值金额转换为中文大写金额
* @param $amount float 金额(分)
* @param $type int 补整类型,0:到角补整;1:到元补整
* @return mixed 中文大写金额
*/
function convertAmountToCn($amount,$type = 1)
{
if ($amount == 0) {
return "零元整";
}
?
if (strlen($amount) > 12) {
return "不支持万亿及更高金额";
}
?
// 预定义中文转换的数组
$digital = array(‘零‘,‘壹‘,‘贰‘,‘叁‘,‘肆‘,‘伍‘,‘陆‘,‘柒‘,‘捌‘,‘玖‘);
// 预定义单位转换的数组
$position = array(‘仟‘,‘佰‘,‘拾‘,‘亿‘,‘仟‘,‘万‘,‘元‘);
?
// 将金额的数值字符串拆分成数组
$amountArr = explode(‘.‘,$amount);
// 将整数位的数值字符串拆分成数组
$integerArr = str_split($amountArr[0],1);
?
// 将整数部分替换成大写汉字
$result = ‘人民币‘;
$integerArrLength = count($integerArr);
$positionLength = count($position);
for ($i=0; $i<$integerArrLength; $i++) {
$result = $result . $digital[$integerArr[$i]]. $position[$positionLength - $integerArrLength + $i];
}
?
// 如果小数位也要转换
if ($type == 1) {
// 将小数位的数值字符串拆分成数组
$decimalArr = str_split($amountArr[1],1);
// 将小数部分替换成大写汉字
$result = $result . $digital[$decimalArr[0]] . ‘角‘ . $digital[$decimalArr[1]] . ‘分‘;
} else {
$result = $result . ‘整‘;
}
?
return $result;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读