PHP价格转大写
<?php
function currencyToChinese($data)
{
$data = str_replace(',', '', $data);
if ($data == '') {
return '';
}
$prefix = '';
$cncap = '';
if ($data < 0) {
$prefix .= '负';
$data = substr($data, 1, strlen($data) - 1);
}
$capnum = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
$capdigit = array("", "拾", "佰", "仟");
$subdata = explode(".", $data);
if (!isset($subdata[1])) {
$subdata[1] = 0;
}
if (intval($subdata[0]) == 0 && intval($subdata[1]) == 0) {
return $prefix . "零元整";
}
$yuan = $subdata[0];
$j = 0; $nonzero = 0;
for ($i = 0; $i < strlen($subdata[0]); $i++) {
if (0 == $i) { //确定个位
if (intval($subdata[1]) != 0) {
if (substr($subdata[0], -1, 1) == 0) {
$cncap .= "元零";
} else {
$cncap .= "元";
}
} else {
if (intval($subdata[0]) != 0) {
$cncap = "元";
}
}
}
if (4 == $i) {
$j = 0;
$nonzero = 0;
$cncap = "万" . $cncap;
} //确定万位
if (8 == $i) {
$j = 0;
$nonzero=0;
$cncap="亿" . $cncap;
} //确定亿位
$numb = substr($yuan, -1, 1); //截取尾数
$cncap = ($numb) ?
$capnum[$numb] . $capdigit[$j] . $cncap : (($nonzero)?"零" . $cncap : $cncap);
$nonzero = ($numb) ? 1 : $nonzero;
$yuan = substr($yuan, 0, strlen($yuan)-1); //截去尾数
$j++;
}
$chiao = '';
$cent = '';
if (intval($subdata[1]) != 0) {
$chiao = (substr($subdata[1], 0, 1)) ?
$capnum[substr($subdata[1], 0, 1)] . "角" : "零";
$cent = (substr($subdata[1], 1, 1)) ?
$capnum[substr($subdata[1], 1, 1)] . "分" : "零分";
}
$cncap .= $chiao.$cent;
if (substr($subdata[1], -1, 1) === '0') {
$cncap .= "整";
}
$cncap = preg_replace("/(零)+/", "\\1", $cncap); //合并连续“零”
if (intval($subdata[0]) == 0) {
$cncap = preg_replace("/(元零)+/", "", $cncap);
$cncap = preg_replace("/(元)+/", "", $cncap);
}
if (intval($subdata[1]) == 0 || substr($subdata[1], -1, 1) === '0') {
$cncap = preg_replace("/(零分)+/", "", $cncap);
}
return $prefix . $cncap;
}
?>
解压密码: detechn或detechn.com
免责声明
本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。