Rumah >pembangunan bahagian belakang >tutorial php >阿拉伯数字转中文
好久没写了,最近刚好用到一个财务上的金额由数字转换为中文的内容,写了个函数实现,记录下:
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 => '拾', ); }
<?php class MoneyFinanceCommon { const MAX_FINANCE_LEN = 10; const FINANCE_SYMBOL = '¥'; const YUAN_POINT = '圆'; // 这里也写了配置,本来应该是统一的,但是因为这个文本基本不会变化,所以就这样写啦 const WAN_POINT = '万'; const YI_POINT = '亿'; /** * 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( * '9' => '', // 仟万 * '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 $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教程有兴趣的朋友有所帮助。