当前位置:Gxlcms > PHP教程 > 阿拉伯数字转中文

阿拉伯数字转中文

时间:2021-07-01 10:21:17 帮助过:46人阅读

好久没写了,最近刚好用到一个财务上的金额由数字转换为中文的内容,写了个函数实现,记录下:

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 => '拾',
    );

}

 '', // 仟万
     *              '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, '');  // 高位用空字符占位
        $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, '.', '');  // 保证金额是精确到分
        $numArr = self::parseNumToArr($sourceNum); 
        if (false !== ($key = array_search('.', $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   = '';
        $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;
    }
    

}

以上就介绍了阿拉伯数字转中文,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行