>  기사  >  백엔드 개발  >  人民币数值转中文大写,人民币中文大写_PHP教程

人民币数值转中文大写,人民币中文大写_PHP教程

WBOY
WBOY원래의
2016-07-12 09:03:121304검색

人民币数值转中文大写,人民币中文大写

<span>class</span><span> Num2RmbClass{
    </span><span>/*</span><span>*
     * 人民币数值转中文大写
     * @author SunsCheung
     * @time 2015.11.11
     * @param string $number 数值 默认为0
     * @param string $int_unit 币种单位,默认"元",有的需求可能为"圆"
     * @param bool $is_round 是否对小数进行四舍五入
     * @param bool $is_extra_zero 是否对整数部分以0结尾,小数存在的数字附加0,比如1960.30,
     *             有的系统要求输出"壹仟玖佰陆拾元零叁角",实际上"壹仟玖佰陆拾元叁角"也是对的
     * @param bool $dec_to_int 是否对让小数部分进位到个位,如果进位,个位加1,小数为0,
     * @return string
     </span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> num2rmb(<span>$number</span> = 0, <span>$int_unit</span> = '元', <span>$is_round</span> = <span>TRUE</span>, <span>$is_extra_zero</span> = <span>FALSE</span>, <span>$dec_to_int</span> = <span>FALSE</span><span>) {
        </span><span>//</span><span> 将数字切分成两段</span>
        <span>$parts</span> = <span>explode</span>('.', <span>$number</span>, 2<span>);
        </span><span>$int</span> = <span>isset</span>(<span>$parts</span>[0]) ? <span>strval</span>(<span>$parts</span>[0]) : '0'<span>;
        </span><span>$dec</span> = <span>isset</span>(<span>$parts</span>[1]) ? <span>strval</span>(<span>$parts</span>[1]) : ''<span>;
        </span><span>//</span><span> 如果小数点后多于2位,不四舍五入就直接截,否则就处理</span>
        <span>$dec_len</span> = <span>strlen</span>(<span>$dec</span><span>);
        </span><span>if</span> (<span>isset</span>(<span>$parts</span>[1]) && <span>$dec_len</span> > 2<span>) {
            </span><span>if</span>(<span>$is_round</span><span>){
                </span><span>if</span>(<span>round</span>(<span>floatval</span>("0.".<span>$dec</span>), 2) == 1 && <span>$dec_to_int</span>){<span>//</span><span>小数进位到个位</span>
                    <span>$int</span> = <span>empty</span>(<span>$int</span>)?1: <span>strval</span>(<span>$parts</span>[0]+1<span>);
                    </span><span>$dec</span> = 0<span>;
                }</span><span>elseif</span>(<span>round</span>(<span>floatval</span>("0.".<span>$dec</span>), 2) == 1){<span>//</span><span>小数不进位到个位</span>
                    <span>$dec</span> = "99"<span>;
                }</span><span>else</span><span>{
                    </span><span>$dec</span> = <span>substr</span>(<span>strrchr</span>(<span>strval</span>(<span>round</span>(<span>floatval</span>("0.".<span>$dec</span>), 2)), '.'), 1<span>);
                    </span><span>echo</span> <span>$dec</span>;<span>die</span>('boss'<span>);
                }
            }</span><span>else</span><span>{
                </span><span>$dec</span> = <span>substr</span>(<span>$parts</span>[1], 0, 2<span>);
            }
        }
        </span><span>//</span><span> 当number为0.001时,小数点后的金额为0元</span>
        <span>if</span> (<span>empty</span>(<span>$int</span>) && <span>empty</span>(<span>$dec</span><span>)) {
            </span><span>return</span> '零'<span>;
        }

        </span><span>//</span><span> 定义</span>
        <span>$chs</span> = <span>array</span>('0','壹','贰','叁','肆','伍','陆','柒','捌','玖'<span>);
        </span><span>$uni</span> = <span>array</span>('','拾','佰','仟'<span>);
        </span><span>$dec_uni</span> = <span>array</span>('角', '分'<span>);
        </span><span>$exp</span> = <span>array</span>('', '万'<span>);
        </span><span>$res</span> = ''<span>;

        </span><span>//</span><span> 整数部分从右向左找</span>
        <span>for</span> (<span>$i</span> = <span>strlen</span>(<span>$int</span>) - 1, <span>$k</span> = 0; <span>$i</span> >= 0; <span>$k</span>++<span>) {
            </span><span>$str</span> = ''<span>;
            </span><span>//</span><span> 按照中文读写习惯,每4个字为一段进行转化,i一直在减</span>
            <span>for</span> (<span>$j</span> = 0; <span>$j</span> < 4 && <span>$i</span> >= 0; <span>$j</span>++, <span>$i</span>--<span>) {
                </span><span>$u</span> = <span>$int</span>{<span>$i</span>} > 0 ? <span>$uni</span>[<span>$j</span>] : ''; <span>//</span><span> 非0的数字后面添加单位</span>
                <span>$str</span> = <span>$chs</span>[<span>$int</span>{<span>$i</span>}] . <span>$u</span> . <span>$str</span><span>;
            }
            </span><span>//</span><span>echo $str."|".($k - 2)."<br>";</span>
            <span>$str</span> = <span>rtrim</span>(<span>$str</span>, '0');<span>//</span><span> 去掉末尾的0</span>
            <span>$str</span> = <span>preg_replace</span>("/0+/", "零", <span>$str</span>); <span>//</span><span> 替换多个连续的0</span>
            <span>if</span> (!<span>isset</span>(<span>$exp</span>[<span>$k</span><span>])) {
                </span><span>$exp</span>[<span>$k</span>] = <span>$exp</span>[<span>$k</span> - 2] . '亿'; <span>//</span><span> 构建单位</span>
<span>            }
            </span><span>$u2</span> = <span>$str</span> != '' ? <span>$exp</span>[<span>$k</span>] : ''<span>;
            </span><span>$res</span> = <span>$str</span> . <span>$u2</span> . <span>$res</span><span>;
        }

        </span><span>//</span><span> 如果小数部分处理完之后是00,需要处理下</span>
        <span>$dec</span> = <span>rtrim</span>(<span>$dec</span>, '0'<span>);

        </span><span>//</span><span> 小数部分从左向右找</span>
        <span>if</span> (!<span>empty</span>(<span>$dec</span><span>)) {
            </span><span>$res</span> .= <span>$int_unit</span><span>;
            </span><span>//</span><span> 是否要在整数部分以0结尾的数字后附加0,有的系统有这要求</span>
            <span>if</span> (<span>$is_extra_zero</span><span>) {
                </span><span>if</span> (<span>substr</span>(<span>$int</span>, -1) === '0'<span>) {
                    </span><span>$res</span>.= '零'<span>;
                }
            }
            </span><span>for</span> (<span>$i</span> = 0, <span>$cnt</span> = <span>strlen</span>(<span>$dec</span>); <span>$i</span> < <span>$cnt</span>; <span>$i</span>++<span>) {
                </span><span>$u</span> = <span>$dec</span>{<span>$i</span>} > 0 ? <span>$dec_uni</span>[<span>$i</span>] : ''; <span>//</span><span> 非0的数字后面添加单位</span>
                <span>$res</span> .= <span>$chs</span>[<span>$dec</span>{<span>$i</span>}] . <span>$u</span><span>;
            }
            </span><span>$res</span> = <span>rtrim</span>(<span>$res</span>, '0');<span>//</span><span> 去掉末尾的0</span>
            <span>$res</span> = <span>preg_replace</span>("/0+/", "零", <span>$res</span>); <span>//</span><span> 替换多个连续的0</span>
        } <span>else</span><span> {
            </span><span>$res</span> .= <span>$int_unit</span> . '整'<span>;
        }
        </span><span>return</span>  <span>$number</span> < 0 ? "(负)".<span>$res</span> : <span>$res</span><span>;
    }

}
</span><span>//</span><span>$a = new Num2RmbClass;</span>
<span>echo</span> (Num2RmbClass::num2rmb('1600020039.9989','圆',<span>false</span>,<span>false</span>,<span>false</span>));<span>//</span><span>壹佰贰拾叁圆肆角伍分</span>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1082223.htmlTechArticle人民币数值转中文大写,人民币中文大写 class Num2RmbClass{ /* * * 人民币数值转中文大写 * @author SunsCheung * @time 2015.11.11 * @param string $number 数值...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.