ade

PHP价格转大写
<?php function currencyToChinese($data) { $data&am...
扫描右侧二维码阅读全文
23
2018/05

PHP价格转大写

<?php
function currencyToChinese($data)
{
    $data&nbsp;=&nbsp;str_replace(&#39;,&#39;,&nbsp;&#39;&#39;,&nbsp;$data);
 
    if ($data == '') {
        return '';
    }
 
    $prefix = '';
    $cncap = '';
 
    if ($data < 0) {
        $prefix .= '负';
        $data&nbsp;=&nbsp;substr($data, 1, strlen($data) - 1);
    }
 
    $capnum = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
    $capdigit = array("", "拾", "佰", "仟");
    $subdata&nbsp;=&nbsp;explode(&quot;.&quot;,&nbsp;$data);
    if (!isset($subdata[1])) {
        $subdata[1] = 0;
    }
 
    if (intval($subdata[0])&nbsp;==&nbsp;0&nbsp;&amp;&amp;&nbsp;intval($subdata[1]) == 0) {
        return $prefix . "零元整";
    }
 
    $yuan&nbsp;=&nbsp;$subdata[0];
    $j&nbsp;=&nbsp;0;&nbsp;$nonzero = 0;
    for ($i&nbsp;=&nbsp;0;&nbsp;$i < strlen($subdata[0]);&nbsp;$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&nbsp;=&nbsp;&quot;万&quot;&nbsp;.&nbsp;$cncap;
        } //确定万位
        if (8 == $i) {
            $j = 0;
            $nonzero=0;
            $cncap=&quot;亿&quot;&nbsp;.&nbsp;$cncap;
        } //确定亿位
        $numb&nbsp;=&nbsp;substr($yuan, -1, 1); //截取尾数
        $cncap&nbsp;=&nbsp;($numb) ?
            $capnum[$numb] . $capdigit[$j] . $cncap&nbsp;:&nbsp;(($nonzero)?"零" . $cncap&nbsp;:&nbsp;$cncap);
        $nonzero&nbsp;=&nbsp;($numb) ? 1 : $nonzero;
        $yuan&nbsp;=&nbsp;substr($yuan, 0, strlen($yuan)-1); //截去尾数
        $j++;
    }
    $chiao = '';
    $cent = '';
    if (intval($subdata[1]) != 0) {
        $chiao&nbsp;=&nbsp;(substr($subdata[1], 0, 1)) ?
            $capnum[substr($subdata[1], 0, 1)] . "角" : "零";
        $cent&nbsp;=&nbsp;(substr($subdata[1], 1, 1)) ?
            $capnum[substr($subdata[1], 1, 1)] . "分" : "零分";
    }
 
    $cncap&nbsp;.=&nbsp;$chiao.$cent;
    if (substr($subdata[1], -1, 1) === '0') {
        $cncap .= "整";
    }
    $cncap&nbsp;=&nbsp;preg_replace(&quot;/(零)+/&quot;,&nbsp;&quot;\\1&quot;,&nbsp;$cncap); //合并连续“零”
 
    if (intval($subdata[0]) == 0) {
        $cncap&nbsp;=&nbsp;preg_replace(&quot;/(元零)+/&quot;,&nbsp;&quot;&quot;,&nbsp;$cncap);
        $cncap&nbsp;=&nbsp;preg_replace(&quot;/(元)+/&quot;,&nbsp;&quot;&quot;,&nbsp;$cncap);
    }
    if (intval($subdata[1])&nbsp;==&nbsp;0&nbsp;||&nbsp;substr($subdata[1], -1, 1) === '0') {
        $cncap&nbsp;=&nbsp;preg_replace(&quot;/(零分)+/&quot;,&nbsp;&quot;&quot;,&nbsp;$cncap);
    }
 
    return $prefix&nbsp;.&nbsp;$cncap;
}
?>
最后修改:2018 年 10 月 23 日 04 : 14 PM
如果觉得我的文章对你有用,请随意赞赏
声明:本站部分资源收集于网络,仅供学习和测试,如果有侵权内容、不妥之处,请联系我们删除。敬请谅解!

发表评论

评论人工审核,请勿发表违规内容、无意义内容,请使用真实邮箱地址,方便接收评论回复及评论审核通知。