好久沒寫了,最近剛好用到一個財務上的金額由數字轉換為中文的內容,寫了個函數實現,記錄下:
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 < 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教程有興趣的朋友有所幫助。