需求是将阿拉伯数字”1″转化为汉字的”一”,”123″转化为”一百二十三”这种.
下面是函数代码:
function numberToChinese($number) { $chineseNumber = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; $chineseUnit = ['', '十', '百', '千', '万', '亿']; $result = ''; $number = strval($number); $length = strlen($number); for ($i = 0; $i < $length; $i++) { $digit = $number[$i]; $unit = $length - $i - 1; if ($digit == 0) { // 如果数字为零,则跳过 continue; } $result .= $chineseNumber[$digit] . $chineseUnit[$unit]; } return $result; } // 示例用法 $number = 123; $chinese = numberToChinese($number); echo $chinese; // 输出:"一百二十三"
记录备忘.