Maison  >  Article  >  php教程  >  人民币数值转中文大写

人民币数值转中文大写

WBOY
WBOYoriginal
2016-07-06 13:31:001876parcourir

class Num2RmbClass{ /* * * 人民币数值转中文大写 * @author SunsCheung * @time 2015.11.11 * @param string $number 数值 默认为0 * @param string $int_unit 币种单位,默认"元",有的需求可能为"圆" * @param bool $is_round 是否对小数进行四舍五入 *

<span style="color: #0000ff;">class</span><span style="color: #000000;"> Num2RmbClass{
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 人民币数值转中文大写
     * @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 style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> num2rmb(<span style="color: #800080;">$number</span> = 0, <span style="color: #800080;">$int_unit</span> = '元', <span style="color: #800080;">$is_round</span> = <span style="color: #0000ff;">TRUE</span>, <span style="color: #800080;">$is_extra_zero</span> = <span style="color: #0000ff;">FALSE</span>, <span style="color: #800080;">$dec_to_int</span> = <span style="color: #0000ff;">FALSE</span><span style="color: #000000;">) {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 将数字切分成两段</span>
        <span style="color: #800080;">$parts</span> = <span style="color: #008080;">explode</span>('.', <span style="color: #800080;">$number</span>, 2<span style="color: #000000;">);
        </span><span style="color: #800080;">$int</span> = <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$parts</span>[0]) ? <span style="color: #008080;">strval</span>(<span style="color: #800080;">$parts</span>[0]) : '0'<span style="color: #000000;">;
        </span><span style="color: #800080;">$dec</span> = <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$parts</span>[1]) ? <span style="color: #008080;">strval</span>(<span style="color: #800080;">$parts</span>[1]) : ''<span style="color: #000000;">;
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果小数点后多于2位,不四舍五入就直接截,否则就处理</span>
        <span style="color: #800080;">$dec_len</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$dec</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$parts</span>[1]) && <span style="color: #800080;">$dec_len</span> > 2<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$is_round</span><span style="color: #000000;">){
                </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">round</span>(<span style="color: #008080;">floatval</span>("0.".<span style="color: #800080;">$dec</span>), 2) == 1 && <span style="color: #800080;">$dec_to_int</span>){<span style="color: #008000;">//</span><span style="color: #008000;">小数进位到个位</span>
                    <span style="color: #800080;">$int</span> = <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$int</span>)?1: <span style="color: #008080;">strval</span>(<span style="color: #800080;">$parts</span>[0]+1<span style="color: #000000;">);
                    </span><span style="color: #800080;">$dec</span> = 0<span style="color: #000000;">;
                }</span><span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">round</span>(<span style="color: #008080;">floatval</span>("0.".<span style="color: #800080;">$dec</span>), 2) == 1){<span style="color: #008000;">//</span><span style="color: #008000;">小数不进位到个位</span>
                    <span style="color: #800080;">$dec</span> = "99"<span style="color: #000000;">;
                }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
                    </span><span style="color: #800080;">$dec</span> = <span style="color: #008080;">substr</span>(<span style="color: #008080;">strrchr</span>(<span style="color: #008080;">strval</span>(<span style="color: #008080;">round</span>(<span style="color: #008080;">floatval</span>("0.".<span style="color: #800080;">$dec</span>), 2)), '.'), 1<span style="color: #000000;">);
                    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$dec</span>;<span style="color: #0000ff;">die</span>('boss'<span style="color: #000000;">);
                }
            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
                </span><span style="color: #800080;">$dec</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$parts</span>[1], 0, 2<span style="color: #000000;">);
            }
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 当number为0.001时,小数点后的金额为0元</span>
        <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$int</span>) && <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$dec</span><span style="color: #000000;">)) {
            </span><span style="color: #0000ff;">return</span> '零'<span style="color: #000000;">;
        }

        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 定义</span>
        <span style="color: #800080;">$chs</span> = <span style="color: #0000ff;">array</span>('0','壹','贰','叁','肆','伍','陆','柒','捌','玖'<span style="color: #000000;">);
        </span><span style="color: #800080;">$uni</span> = <span style="color: #0000ff;">array</span>('','拾','佰','仟'<span style="color: #000000;">);
        </span><span style="color: #800080;">$dec_uni</span> = <span style="color: #0000ff;">array</span>('角', '分'<span style="color: #000000;">);
        </span><span style="color: #800080;">$exp</span> = <span style="color: #0000ff;">array</span>('', '万'<span style="color: #000000;">);
        </span><span style="color: #800080;">$res</span> = ''<span style="color: #000000;">;

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

        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果小数部分处理完之后是00,需要处理下</span>
        <span style="color: #800080;">$dec</span> = <span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$dec</span>, '0'<span style="color: #000000;">);

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

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

 


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn