PHP 数字转换中文
------------恢复内容开始------------
1.在最近的开发需求因为要用到类似支付宝或者微信收款声音模拟,要将金额改为中文,所以做一下处理
2.思路是已小数点为区分,比如是0.45元就是零点四五元,具体如下,如有优化的希望大佬指点:
$num = '153.2';//"一百五十三点二 function cc($num){ if(strpos($num,'.') !== false)//如果是小数点显示 "一百五十三点二 { $number = explode('.',$num); $num1 = num2cn($number[0]); $num2 = ''; $num_float = str_split($number[1], 1); foreach ($num_float as $key => &$value) { $num2 .= num2cn($value); } dd($num1.'点'.$num2); }else{ dd(num2cn($num)); } } function num2cn($number) { $number = intval ( $number ); $capnum = array ( "零", "一","二","三", "四", "五", "六", "七","八","九" ); $capdigit = array ( "", "十","百","千","万"); $data_arr = str_split ( $number ); $count = count ( $data_arr ); for($i = 0; $i < $count; $i ++) { $d = $capnum [$data_arr [$i]]; $arr [] = $d != '零' ? $d . $capdigit [$count - $i - 1] : $d; } $cncap = implode ( "", $arr ); $cncap = preg_replace ( "/(零)+/", "0", $cncap ); // 合并连续“零” $cncap = trim ( $cncap, '0' ); $cncap = str_replace ( "0", "零", $cncap ); // 合并连续“零” $cncap == '一十' && $cncap = '十'; $cncap == '' && $cncap = '零'; return $cncap; }