An amount conversion function used in CRM in the early years. Today, take it out of the old project and record it. There are many functional methods for amount conversion, which are very good. But this is a conversion function that Cui wrote when he was working. It's more or less memorable. If you have any questions, please point out that Cui will correct them in time. Thank you!
Don't talk too much, just go to the code:
1 <?php 2 3 /** 4 * Conversion of digital amount to capital number 5 * $num Numeric type 6 */ 7 8 function inttodaxie($num) { 9 //judge $num Whether the number 10 if(!is_numeric($num)) return -1; 11 $dint = array('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'); 12 $len = strlen($num); 13 $dstr = ''; 14 for($i = 0; $i <= $len; $i++) { 15 $key_ = substr($num, $i, 1); 16 $dstr .= $dint[$key_]; 17 } 18 return $dstr; 19 } 20 21 $result = inttodaxie(90011234); 22 var_dump($result); 23 echo '<br>';
The above is the basic conversion code, on which the secondary transformation is carried out:
1 <?php 2 3 /** 4 * Amount in words 5 * $num Numeric type 6 */ 7 8 function inttod($num) { 9 //judge $num Does it exist? 10 if(!$num) return 'Zero circle'; 11 //Keep two decimal places 12 $num = round($num, 2); 13 //Convert floating point to integer 14 $tem_num = $num * 100; 15 //Judge number length 16 $tem_num_len = strlen($tem_num); 17 if($tem_num_len > 14) { 18 return 'It's too big. Is there such a large amount of money'; 19 } 20 21 //Capitalized number 22 $dint = array('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'); 23 //Amount unit in words 24 $danwei = array('Thousand', 'Bai', 'Ten', 'Billion', 'Thousand', 'Bai', 'Ten', 'ten thousand', 'Thousand', 'Bai', 'Ten', 'circular'); 25 $danwei1 = array('horn', 'branch'); 26 27 //Empty variable to hold the conversion string 28 $daxie = ''; 29 30 //Divide numbers, divide fillets 31 list($left_num, $right_num) = explode('.', $num); 32 33 //Calculate unit length 34 $danwei_len = count($danwei); 35 //Calculate the length of the split string 36 $left_num_len = strlen($left_num); 37 $right_num_len = strlen($right_num); 38 39 //Circulation calculation of billions of yuan, etc 40 for($i = 0; $i < $left_num_len; $i++) { 41 //Loop single text 42 $key_ = substr($left_num, $i, 1); 43 44 //If it is judged that the number is not equal to 0 or the number is equal to 0 and the amount unit is billion, ten thousand or circle, the string of the complete unit will be returned 45 if($key_ !== '0' || ($key_ == '0' && ($danwei[$danwei_len - $left_num_len + $i] == 'Billion' || $danwei[$danwei_len - $left_num_len + $i] == 'ten thousand' || $danwei[$danwei_len - $left_num_len + $i] == 'circular'))) { 46 $daxie = $daxie . $dint[$key_] . $danwei[$danwei_len - $left_num_len + $i]; 47 } else { 48 //Otherwise, the unit is not included 49 $daxie = $daxie . $dint[$key_]; 50 } 51 } 52 53 //Cycle calculation angle 54 for($i = 0; $i < $right_num_len; $i++) { 55 $key_ = substr($right_num, $i, 1); 56 if($key_ > 0) { 57 $daxie = $daxie . $dint[$key_] . $danwei1[$i]; 58 } 59 } 60 61 //Calculate converted length 62 $daxie_len = strlen($daxie); 63 //Set the text slice to start at 0, utf-8 Chinese characters take up 3 characters 64 $j = 0; 65 while($daxie_len > 0) { 66 //Slice two Chinese characters at a time 67 $str = substr($daxie, $j, 6); 68 //Judge that the text after slicing is not equal to 0 million, 0 yuan, 0 billion, 0 zero 69 if($str == 'Zero' || $str == 'Zero circle' || $str == 'Zero' || $str == '00') { 70 //Re slice 71 $left = substr($daxie, 0, $j); 72 $right = substr($daxie, $j + 3); 73 $daxie = $left . $right; 74 } 75 $j += 3; 76 $daxie_len -= 3; 77 } 78 79 return 'RMB' . $daxie . 'whole'; 80 } 81 82 $result = inttod(99090909090.19); 83 var_dump($result);
Operation result:
Source code uploaded to GitHub: https://github.com/cuiyunxin/php-demo/blob/master/capital.php
Please comment if there is any mistake. Thank you.