PHP算法--将数字金额转换成大写金额
web PHP 2019-05-06 1832 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6457894 2613 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)

注:这个小算法适用于10万以内的金额。

<?php 
// $num = 12345.67;
function RMB_Upper($num) {
    $num = round($num, 2); //取两位小数
    $num = '' . $num; //转换成数字
    $arr = explode('.', $num);
    $str_left = $arr[0]; // 12345
    $str_right = $arr[1]; // 67
    $len_left = strlen($str_left); //小数点左边的长度
    $len_right = strlen($str_right); //小数点右边的长度 
    // 循环将字符串转换成数组,
    for($i = 0;$i < $len_left;$i++) {
        $arr_left[] = substr($str_left, $i, 1);
    } 
    // print_r($arr_left);
    // output:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
    for($i = 0;$i < $len_right;$i++) {
        $arr_right[] = substr($str_right, $i, 1);
    } 
    // print_r($arr_right);
    // output:Array ( [0] => 6 [1] => 7 )
    // 构造数组$daxie
    $daxie = array('0' => '零',
        '1' => '壹',
        '2' => '贰',
        '3' => '叁',
        '4' => '肆',
        '5' => '伍',
        '6' => '陆',
        '7' => '柒',
        '8' => '捌',
        '9' => '玖',
        ); 
    // 循环将数组$arr_left中的值替换成大写
    foreach($arr_left as $k => $v) {
        $arr_left[$k] = $daxie[$v];
        switch ($len_left--) {
            // 数值后面追加金额单位
            case 5:
                $arr_left[$k] .= '万';
                break;
            case 4:
                $arr_left[$k] .= '千';
                break;
            case 3:
                $arr_left[$k] .= '百';
                break;
            case 2:
                $arr_left[$k] .= '十';
                break;
            default:
                $arr_left[$k] .= '元';
                break;
        } 
    } 
    // print_r($arr_left);
    // output :Array ( [0] => 壹万 [1] => 贰千 [2] => 叁百 [3] => 肆十 [4] => 伍元 )
    foreach($arr_right as $k => $v) {
        $arr_right[$k] = $daxie[$v];
        switch ($len_right--) {
            case 2:
                $arr_right[$k] .= '角';
                break;
            default:
                $arr_right[$k] .= '分';
                break;
        } 
    } 
    // print_r($arr_right);
    // output :Array ( [0] => 陆角 [1] => 柒分 )
    // 将数组转换成字符串,并拼接在一起
    $new_left_str = implode('', $arr_left);
    $new_right_str = implode('', $arr_right);
    $new_str = $new_left_str . $new_right_str; 
    // echo $new_str;
    // output :'壹万贰千叁百肆十伍元陆角柒分'
    // 如果金额中带有0,大写的字符串中将会带有'零千零百零十',这样的字符串,需要替换掉
    $new_str = str_replace('零万', '零', $new_str);
    $new_str = str_replace('零千', '零', $new_str);
    $new_str = str_replace('零百', '零', $new_str);
    $new_str = str_replace('零十', '零', $new_str);
    $new_str = str_replace('零零零', '零', $new_str);
    $new_str = str_replace('零零', '零', $new_str);
    $new_str = str_replace('零元', '元', $new_str); 
    // echo'<br/>';
    return $new_str;
} 
echo RMB_Upper(12345.67); // 壹万贰千叁百肆十伍元陆角柒分

版权声明:本篇文章来源于网络。 来源链接

相关推荐
php实现ZIP压缩文件解压缩,中文乱码解决方法(重要)
PHP | 2019-04-30 5637
直接上代码,具体代码里面都有注释。直接中文压缩文件解压到中文文件夹。 <?php // 需开启配置 php_zip.dll // phpinfo(); header("Content-type:text/html;charset=utf-8"); /* * $filename 被解压文件名 * $path 解压...
openssl_private_decrypt解密失败
PHP | 2019-01-16 5608
复制别人的解密程序,原程序可以解密,复制过来就不可以 一步步尝试发现,秘钥换行符有区别, 原秘钥有换行符,复制过来不知道什么时候把换行符清空了,成了一行字符串了。
评论:0条
评论加载中...
发表评论