Arabic numerals to Chinese

WBOY
Release: 2016-08-08 09:32:40
Original
1956 people have browsed it

I haven’t written for a long time. Recently I just needed to convert a financial amount from numbers to Chinese. I wrote a function to implement it and recorded it:

class MoneyConstConfig
{

    /**
     * 金额数字中文映射
     */
    public static $NUM_CH_MAP = array(
        0 => '零',
        1 => '壹',
        2 => '贰',
        3 => '叁',
        4 => '肆',
        5 => '伍',
        6 => '陆',
        7 => '柒',
        8 => '捌',
        9 => '玖',
    );


    /**
     * 金额层次中文映射 
     */
    public static $FINANCE_UNIT_MAP = array(
        0  => '分',
        1  => '角',
        2  => '圆',
        3  => '拾',
        4  => '佰',
        5  => '仟',
        6  => '万',
        7  => '拾',
        8  => '佰',
        9  => '仟',
        10 => '亿',
        11 => '拾',
    );

}
Copy after login

<?php
class MoneyFinanceCommon
{

    const MAX_FINANCE_LEN = 10;
    const FINANCE_SYMBOL  = &#39;¥&#39;;
    const YUAN_POINT      = &#39;圆&#39;;  // 这里也写了配置,本来应该是统一的,但是因为这个文本基本不会变化,所以就这样写啦
    const WAN_POINT       = &#39;万&#39;;
    const YI_POINT        = &#39;亿&#39;;

    /**
     * parseNumToArr 将数字转换位数组
     * 
     * @param double $num 
     * @static
     * @access public
     * @return void
     */
    public static function parseNumToArr($num)
    {
        return str_split($num); 
    }


    /**
     * ticketFinanceChDesc 票据金额中文描述
     * 
     * @param double $sourceNum
     * @static
     * @access public
     * @return array(
     *              &#39;9&#39; => '', // 仟万
     *              '8' => '', // 佰万
     *              '7' => '', // 拾万
     *              '6' => '', // 万
     *              '5' => '', // 仟
     *              '4' => '', // 佰
     *              '3' => '', // 拾
     *              '2' => '', // 圆
     *              '1' => '', // 角
     *              '0' => '', // 分
     *          );
     */
    public static function ticketFinanceChDesc($sourceNum)                                                                   [67/1580]
    {
        $sourceNum = number_format($sourceNum, 2, '.', '');  // 保证金额是精确到分
        $numArr = self::parseNumToArr($sourceNum); 
        if (false !== ($key = array_search('.', $numArr))) {
            unset($numArr[$key]); 
        }
        
        $rnumArr = array_reverse($numArr);
        $data = array();
        $i = 0;
        foreach ($rnumArr as $num) {
            $data[$i++] = $num;
        }
        
        if ($i < self::MAX_FINANCE_LEN) {
            $data[] = self::FINANCE_SYMBOL; 
        }
        $data = array_pad($data, self::MAX_FINANCE_LEN, &#39;&#39;);  // 高位用空字符占位
        $result = array_reverse($data);
        
        return $result;
    }
                                                                                                                             [43/1580]
    /**
     * financeNumToCh 
     * 
     * @param double $sourceNum
     * @static
     * @access public
     * @return string $chAmount
     */
    public static function financeNumToCh($sourceNum)
    {
        $sourceNum = number_format($sourceNum, 2, &#39;.&#39;, &#39;&#39;);  // 保证金额是精确到分
        $numArr = self::parseNumToArr($sourceNum); 
        if (false !== ($key = array_search(&#39;.&#39;, $numArr))) {
            unset($numArr[$key]); 
        }

        $amount = array();
        $numArrRev = array_reverse($numArr);
        $i = 0;
        foreach ($numArrRev as $num) {
            $amount[$i++]  = $num;
        }
        $amount = array_reverse($amount, true);  // 这里再一次反转目的是保证key 的顺序
        $chAmount = self::convertNumArrToChStr($amount);
        return $chAmount;
    }
    /**                                                                                                                      [15/1580]
     * convertNumArrToChStr 将数字数组转换位中文数组
     * 
     * @param array $numArr  待转换的数字数组
     * @static
     * @access public
     * @return string $chStr
     */
    public static function convertNumArrToChStr($numArr)
    {
        $data    = array();
        $exists0 = false;  // 是否存在0 
        $chStr   = &#39;&#39;;
        $unitMap = array_flip(MoneyConstConfig::$FINANCE_UNIT_MAP);
        $yuan = $unitMap[self::YUAN_POINT];
        $wan  = $unitMap[self::WAN_POINT];
        $yi   = $unitMap[self::YI_POINT];
        foreach ($numArr as $key => $num) {
            if ($num != 0) {
                if ($exists0 === true) {
                    $chStr   .= '零';
                    $exists0 = false;
                }
                $chStr .= MoneyConstConfig::$NUM_CH_MAP[$num] . MoneyConstConfig::$FINANCE_UNIT_MAP[$key]; 
            } else {
                $exists0 = true; 
            } 

            if ($key == $yuan) {
                $chStr = rtrim($chStr, self::YUAN_POINT);
                $chStr .= self::YUAN_POINT;
            } elseif ($key == $wan) {
                $chStr = rtrim($chStr, self::WAN_POINT); 
                $chStr .= self::WAN_POINT;
            } elseif ($key == $yi) {
                $chStr = rtrim($chStr, self::YI_POINT); 
                $chStr .= self::YI_POINT;
            }
        }   
        return $chStr;
    }
    

}
Copy after login

The above has introduced the conversion of Arabic numerals to Chinese, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!