20190303, 今天在博客園看到有文章在講"PHP數字金額轉換大寫金額", 於是我抽時間也寫了一個. 不多說, 把代碼發上來:
1 /** 2 * 將數值金額轉換為中文大寫金額 3 * @param $amount float 金額(分) 4 * @param $type int 補整類型,0:到角補整;1:到元補整 5 * @return mixed 中文大寫金額 6 */ 7 function convertAmountToCn($amount, $type = 1) { 8 if ($amount == 0) { 9 return "零元整"; 10 } 11 if (strlen($amount) > 12) { 12 return "不支持萬億及更高金額"; 13 } 14 15 // 預定義中文轉換的數組 16 $digital = array('零', '壹', '貳', '叄', '肆', '伍', '陸', '柒', '捌', '玖'); 17 // 預定義單位轉換的數組 18 $position = array('仟', '佰', '拾', '億', '仟', '佰', '拾', '萬', '仟', '佰', '拾', '元'); 19 20 // 將金額的數值字符串拆分成數組 21 $amountArr = explode('.', $amount); 22 23 // 將整數位的數值字符串拆分成數組 24 $integerArr = str_split($amountArr[0], 1); 25 // 將整數部分替換成大寫漢字 26 $result = '人民幣'; 27 $integerArrLength = count($integerArr); 28 $positionLength = count($position); 29 for($i=0; $i<$integerArrLength; $i++){ 30 $result = $result . $digital[$integerArr[$i]]. $position[$positionLength - $integerArrLength + $i]; 31 } 32 33 // 如果小數位也要轉換 34 if($type == 1){ 35 // 將小數位的數值字符串拆分成數組 36 $decimalArr = str_split($amountArr[1], 1); 37 // 將小數部分替換成大寫漢字 38 $result = $result . $digital[$decimalArr[0]] . '角' . $digital[$decimalArr[1]] . '分'; 39 }else{ 40 $result = $result . '整'; 41 } 42 43 return $result; 44 }
20190305, 經過考慮后, 發現之前寫的代碼不夠嚴謹, 特此補充了一份新版的代碼
1 /** 2 * 將數值金額轉換為中文大寫金額 3 * @param $amount float 金額(支持到分) 4 * @param $type int 補整類型,0:到角補整;1:到元補整 5 * @return mixed 中文大寫金額 6 */ 7 function convertAmountToCn($amount, $type = 1) { 8 // 判斷輸出的金額是否為數字或數字字符串 9 if(!is_numeric($amount)){ 10 return "要轉換的金額只能為數字!"; 11 } 12 13 // 金額為0,則直接輸出"零元整" 14 if($amount == 0) { 15 return "人民幣零元整"; 16 } 17 18 // 金額不能為負數 19 if($amount < 0) { 20 return "要轉換的金額不能為負數!"; 21 } 22 23 // 金額不能超過萬億,即12位 24 if(strlen($amount) > 12) { 25 return "要轉換的金額不能為萬億及更高金額!"; 26 } 27 28 // 預定義中文轉換的數組 29 $digital = array('零', '壹', '貳', '叄', '肆', '伍', '陸', '柒', '捌', '玖'); 30 // 預定義單位轉換的數組 31 $position = array('仟', '佰', '拾', '億', '仟', '佰', '拾', '萬', '仟', '佰', '拾', '元'); 32 33 // 將金額的數值字符串拆分成數組 34 $amountArr = explode('.', $amount); 35 36 // 將整數位的數值字符串拆分成數組 37 $integerArr = str_split($amountArr[0], 1); 38 39 // 將整數部分替換成大寫漢字 40 $result = '人民幣'; 41 $integerArrLength = count($integerArr); // 整數位數組的長度 42 $positionLength = count($position); // 單位數組的長度 43 for($i = 0; $i < $integerArrLength; $i++) { 44 // 如果數值不為0,則正常轉換 45 if($integerArr[$i] != 0){ 46 $result = $result . $digital[$integerArr[$i]] . $position[$positionLength - $integerArrLength + $i]; 47 }else{ 48 // 如果數值為0, 且單位是億,萬,元這三個的時候,則直接顯示單位 49 if(($positionLength - $integerArrLength + $i + 1)%4 == 0){ 50 $result = $result . $position[$positionLength - $integerArrLength + $i]; 51 } 52 } 53 } 54 55 // 如果小數位也要轉換 56 if($type == 0) { 57 // 將小數位的數值字符串拆分成數組 58 $decimalArr = str_split($amountArr[1], 1); 59 // 將角替換成大寫漢字. 如果為0,則不替換 60 if($decimalArr[0] != 0){ 61 $result = $result . $digital[$decimalArr[0]] . '角'; 62 } 63 // 將分替換成大寫漢字. 如果為0,則不替換 64 if($decimalArr[1] != 0){ 65 $result = $result . $digital[$decimalArr[1]] . '分'; 66 } 67 }else{ 68 $result = $result . '整'; 69 } 70 return $result; 71 }
本文原創自老劉博客 http://laoliu.pro/article/27