拓讯达通信 发表于 2024-6-14 01:09:04

实用小工具:数字金额转人民币/美元大写


    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">国庆节后的第一个工作日,依旧是日常忙。各种需求犹如节后大爆发一样 扑面而来。于是乎,忙于完成需求以及解决各类问题。但是,有颗好奇心的我,怎么会仅仅满足于完成需求,而不在其中找到一些有趣功能转为实用的小工具呢。</p>
    <h1 style="text-align: left; margin-bottom: 10px;">数字金额转人民币/美元大写的由来</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">电商、金融类网站是和钱打交道最多的,而电商类在处理订单类更是需要对其进行打印清单,并要求对上面的金额不仅要求是数字,而且还需要人民币数字大写。设置多币种的话,还需要转成对应国家语言的数字金额。着实要花点精力、费点脑子。</p>
    <h1 style="text-align: left; margin-bottom: 10px;">实践,匠人精神</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">于是,我既然在完成这个功能的基础上,还是需要基于多种情况进行完善下。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">先定义金额转大写 MoneyConvert类,然后</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">需要 数字金额转人民币大写 就有了:</p>$money = <span style="color: green;">100050.23</span>;

    <span style="color: green;">echo</span> MoneyConvert::numbertowords(MoneyConvert::MONEY_TYPE_RMB,$money);

    输入金额:<span style="color: green;">100050.23</span>。转换成大写为: 壹拾万零伍拾元贰角叁分<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">需要 数字金额转美元大写(美分表达):</p>$money = <span style="color: green;">100050.23</span>;

    <span style="color: green;">echo</span> MoneyConvert::numbertowords(MoneyConvert::MONEY_TYPE_DOLLAR,$money,<span style="color: green;">cents</span>);

    输入金额:<span style="color: green;">100050.23</span>。转换成大写为: ONE HUNDRED THOUSAND,FIFTY <span style="color: green;">AND</span> CENTS TWENTY-THREE ONLY【美分表达(数字转换到文字)】<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">需要 数字金额转美元大写(美点表达) :</p>$money = <span style="color: green;">100050.23</span>;

    <span style="color: green;">echo</span> MoneyConvert::numbertowords(MoneyConvert::MONEY_TYPE_DOLLAR,$money,<span style="color: green;">point</span>);

    输入金额:<span style="color: green;">100050.23</span>。转换成大写为: ONE HUNDRED THOUSAND,FIFTY <span style="color: green;">AND</span> POINT TWENTY-THREE ONLY【美点表达(拼出大写字母)】
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">最后是 数字金额转美元大写(分数表达法):</p>$money = <span style="color: green;">100050.23</span>;

    <span style="color: green;">echo</span> MoneyConvert::numbertowords(MoneyConvert::MONEY_TYPE_DOLLAR,$money,<span style="color: green;">fraction</span>);

    输入金额:<span style="color: green;">100050.23</span>。转换成大写为: ONE HUNDRED THOUSAND,FIFTY <span style="color: green;">AND</span> TWENTY-THREE【分数表达法(只接受数字)】

    <h1 style="text-align: left; margin-bottom: 10px;">总结</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">数字金额转人民币/美元大写,本身可能不复杂。但是,确实实用。把金额转人民币/美元大写结合场景,发散下思维就能写出一个相对较全的功能。下次再用的时候就方便多了~</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">参考链接:</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">https://github.com/masonyang/smalltools</p>


页: [1]
查看完整版本: 实用小工具:数字金额转人民币/美元大写