PHP小写转大写金额

直接贴代码

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <form method="post" action="#">
        <input name="money" value="<?php if(isset($_POST['act'])){echo $_POST['money'];}?>"></input>
        <input type="submit" value="开始转换" name="act"></input>
    </form>

</body>
</html>

<?php
    function change_num($i){
        switch($i){
            case '1': return "壹";
            case '2': return "贰";
            case '3': return "叁"; 
            case '4': return "肆";
            case '5': return "伍";
            case '6': return "陆";
            case '7': return "柒";
            case '8': return "捌";
            case '9': return "玖";
            case '0': return "零";
            defualt : return "参数错误";exit();
        }
    }

    function change_int1($i){
        switch($i){
            case '1':return "";
            case '2': return "拾";
            case '3': return "佰";
            case '0': return "千";
            defualt : return "参数错误";exit;
        }
    }

    function change_int2($i){
        switch($i){
            case '0': return "圆";
            case '1': return "万";
            case '2': return "亿"; 
            defualt : return "参数错误";exit;
        }
    }

    function change_flo($i){
        switch($i){
            case '1': return "角";
            case '2': return "分";
            defualt : return "参数错误";exit;
        }
    }

    $big_mon = "";
    if(isset($_POST['act'])){

        if(!is_numeric($_POST['money'])){
            exit("不是数字");
        }

        $money = $_POST['money'];

        $money_int = intval($money);

        $money_float = round(($money - $money_int),2);

        $len_int = strlen($money_int);
        $len_float = strlen($money_float);

        for($i=0;$i<$len_int;$i++){
            if($money[$i] != 0){
                $big_mon .= change_num($money[$i]).change_int1(($len_int-$i)%4);
            }elseif(isset($money[$i+1]) && $money[$i+1] !=0){
                $big_mon .= change_num($money[$i]);
            }

            if(($len_int-$i)%4==1){
                $big_mon .= change_int2(intval(($len_int-$i)/4));
            }
        }

        $n_jiao = intval($money_float*10);
        $n_fen = $money_float*100 - $n_jiao*10; 

        if($n_jiao != 0){
            $big_mon .= change_num($n_jiao).change_flo(1);
        }elseif($n_fen != 0){
            $big_mon .= change_num($n_jiao);
        }

        if($n_fen != 0){
            $big_mon .= change_num($n_fen).change_flo(2);
        }

    }
    echo $big_mon;
?>
tag(s): PHP
show comments · back · home
Edit with Markdown