PHP 數字轉大寫
阿新 • • 發佈:2018-02-09
bnu 謝謝 bst mrm ++ form htm har out
<?php header("content-type:text/html;charset=utf-8"); function numToRmb($num){ $rmbNum = [‘零‘,‘壹‘,‘貳‘,‘叁‘,‘肆‘,‘伍‘,‘陸‘,‘柒‘,‘捌‘,‘玖‘]; $rmbUnit = [‘分‘,‘角‘,‘元‘,‘拾‘,‘佰‘,‘仟‘,‘萬‘,‘拾‘,‘佰‘,‘仟‘,‘億‘,‘拾‘,‘佰‘,‘仟‘,‘萬‘,‘拾‘,‘佰‘,‘仟‘]; //數字預處理 if($num == ‘‘ || $num == 0){ return ‘請輸入一個大於0的金額,謝謝!‘; }else {$num = number_format($num, 2, ‘.‘, ‘‘)*100; } if (strlen(number_format($num, 0, ‘‘, ‘‘))>18) { return ‘輸入的金額不得超過千萬億!‘; } //開始數字轉換人民幣大寫 $out1 = 0; $numRmb = ‘‘; $unit = ‘‘; $rmb = ‘‘; for ($out1 = 0 ; $out1 < strlen(number_format($num, 0, ‘‘, ‘‘)) ; $out1++) { $numRmb = $rmbNum[substr(number_format($num, 0, ‘‘, ‘‘), $out1, 1)]; $unit = $rmbUnit[strlen(number_format($num, 0, ‘‘, ‘‘))-$out1-1]; $rmb = $rmb.$numRmb.$unit; } // 將大寫人民幣中的零進行調整 $rmbStrlen = strlen($rmb); for ($out2=0; $out2 < $rmbStrlen; $out2=$out2+3) { $tempRmb = substr($rmb, $out2, 6); if($tempRmb==‘零元‘||$tempRmb==‘零萬‘||$tempRmb==‘零億‘) { $left = substr($rmb, 0, $out2); $right = substr($rmb, $out2+3); $rmb = $left.$right; $out2 = $out2 - 3; $rmbStrlen = $rmbStrlen - 3; }elseif ($tempRmb==‘零拾‘||$tempRmb==‘零佰‘||$tempRmb==‘零仟‘) { $left = substr($rmb, 0, $out2+3); $right = substr($rmb, $out2+6); $rmb = $left.$right; $out2 = $out2 - 3; $rmbStrlen = $rmbStrlen - 3; }elseif ($tempRmb==‘零分‘||$tempRmb==‘零角‘) { $left = substr($rmb, 0, $out2); $right = substr($rmb, $out2+6); $rmb = $left.$right; $out2 = $out2 - 6; $rmbStrlen = $rmbStrlen - 6; } if ($tempRmb==‘零零‘) { $left = substr($rmb, 0, $out2); $right = substr($rmb, $out2+3); $rmb = $left.$right; $out2 = $out2 - 3; $rmbStrlen = $rmbStrlen - 3; } if(substr($rmb,strlen($rmb)-3)==‘元‘) { $rmb = $rmb.‘整‘; } } return $rmb; } echo numToRmb($_POST[‘num‘]); ?>
PHP 數字轉大寫