解决发票系统中,发票单上需要填写中文金额的问题:
function ToChineseNum($num) { $zh_num = [‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘, ‘柒‘, ‘捌‘, ‘玖‘]; $zh_unit = [‘分‘, ‘角‘, ‘元‘, ‘拾‘, ‘佰‘, ‘仟‘, ‘万‘, ‘拾‘, ‘佰‘, ‘仟‘, ‘亿‘, ‘拾‘, ‘佰‘, ‘仟‘]; if (!is_numeric(str_replace(‘,‘, ‘‘, $num))) { return $num; } $number = strrev(round(str_replace(‘,‘, ‘‘, $num), 2) * 100); $length = strlen($number); $ch_str = ‘‘; for ($length; $length > 0; $length--) { $index = $length - 1; if ($number[$index] == ‘0‘ && !in_array($zh_unit[$index], [‘万‘, ‘元‘, ‘亿‘])) { $ch_str.=$zh_num[$number[$index]]; } elseif ($number[$index] == ‘0‘ && in_array($zh_unit[$index], [‘万‘, ‘元‘, ‘亿‘])) { $ch_str.= $zh_unit[$index]; } else { $ch_str.=$zh_num[$number[$index]] . $zh_unit[$index]; } } $format_str = trim(str_replace([‘零零‘, ‘零万‘, ‘零元‘, ‘零亿‘], [‘零‘, ‘万‘, ‘元‘, ‘亿‘], $ch_str), ‘零‘); if (preg_match(‘/(分|角)/‘, $format_str) === 0) { $format_str.=‘整‘; } return $format_str; }
原文地址:http://suiwnet.blog.51cto.com/2492370/1794993