├── baht_text.php └── readme.md /baht_text.php: -------------------------------------------------------------------------------- 1 | 5) { 26 | $millions = floor($log / 6); 27 | $million_value = pow(1000000, $millions); 28 | $normalised_million = floor($number / $million_value); 29 | $rest = $number - ($normalised_million * $million_value); 30 | $millions_text = ''; 31 | for ($i = 0; $i < $millions; $i++) { 32 | $millions_text .= BAHT_TEXT_UNITS[6]; 33 | } 34 | return baht_text($normalised_million, false) . $millions_text . baht_text($rest, true, false); 35 | } 36 | 37 | $number_str = (string)floor($number); 38 | $text = ''; 39 | $unit = 0; 40 | 41 | if ($display_zero && $number_str == '0') { 42 | $text = BAHT_TEXT_NUMBERS[0]; 43 | } else for ($i = strlen($number_str) - 1; $i > -1; $i--) { 44 | $current_number = (int)$number_str[$i]; 45 | 46 | $unit_text = ''; 47 | if ($unit == 0 && $i > 0) { 48 | $previous_number = isset($number_str[$i - 1]) ? (int)$number_str[$i - 1] : 0; 49 | if ($current_number == 1 && $previous_number > 0) { 50 | $unit_text .= BAHT_TEXT_ONE_IN_TENTH; 51 | } else if ($current_number > 0) { 52 | $unit_text .= BAHT_TEXT_NUMBERS[$current_number]; 53 | } 54 | } else if ($unit == 1 && $current_number == 2) { 55 | $unit_text .= BAHT_TEXT_TWENTY; 56 | } else if ($current_number > 0 && ($unit != 1 || $current_number != 1)) { 57 | $unit_text .= BAHT_TEXT_NUMBERS[$current_number]; 58 | } 59 | 60 | if ($current_number > 0) { 61 | $unit_text .= BAHT_TEXT_UNITS[$unit]; 62 | } 63 | 64 | $text = $unit_text . $text; 65 | $unit++; 66 | } 67 | 68 | if ($include_unit) { 69 | $text .= BAHT_TEXT_BAHT; 70 | 71 | $satang = explode('.', number_format($number, 2, '.', ''))[1]; 72 | $text .= $satang == 0 73 | ? BAHT_TEXT_INTEGER 74 | : baht_text($satang, false) . BAHT_TEXT_SATANG; 75 | } else { 76 | $exploded = explode('.', $number); 77 | if (isset($exploded[1])) { 78 | $text .= BAHT_TEXT_POINT; 79 | $decimal = (string)$exploded[1]; 80 | for ($i = 0; $i < strlen($decimal); $i++) { 81 | $text .= BAHT_TEXT_NUMBERS[$decimal[$i]]; 82 | } 83 | } 84 | } 85 | 86 | return $text; 87 | } 88 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # php-baht_text 2 | 3 | ฟังก์ชัน `baht_text` ใช้สำหรับแปลงจำนวนเงินให้เป็นข้อความ ในภาษา PHP 4 | 5 | `baht_text` is a PHP function that converts a number into Thai text with baht currency format (as `BAHTTEXT()` function in Excel). 6 | 7 | ## พารามีเตอร์ 8 | 9 | 1. **`double|int $number`** - จำนวนเงิน 10 | 2. **`bool $include_unit`** - เพิ่มหน่วยบาท/สตางค์หรือไม่ (ค่าเริ่มต้น: `true`) 11 | 3. **`bool $display_zero`** - แสดงผลข้อความสำหรับเลข 0 หรือไม่ เช่น ศูนย์บาทถ้วน (ค่าเริ่มต้น: `true`) 12 | 13 | ## การคืนค่า 14 | 15 | จะคืนค่าเป็น `string` ของข้อความจำนวนเงินที่แปลงแล้ว แต่หากอินพุตไม่ใช่ตัวเลขจะคืนค่าเป็น `null` 16 | 17 | ## ตัวอย่าง 18 | 19 | ### ตัวอย่าง 1 20 | ``` 21 | echo baht_text(54627); // ห้าหมื่นสี่พันหกร้อยยี่สิบเจ็ดบาทถ้วน 22 | ``` 23 | 24 | ### ตัวอย่าง 2 25 | ``` 26 | echo baht_text('361.75'); // สามร้อยหกสิบเอ็ดบาทเจ็ดสิบห้าสตางค์ 27 | ``` 28 | 29 | ### ตัวอย่าง 3 30 | ``` 31 | echo baht_text(5001, false); // ห้าพันหนึ่ง 32 | ``` 33 | 34 | ### ตัวอย่าง 4 35 | ``` 36 | echo baht_text(532729.78, false); // ห้าแสนสามหมื่นสองพันเจ็ดร้อยยี่สิบเก้าจุดเจ็ดแปด 37 | ``` 38 | 39 | ### ตัวอย่าง 5 40 | ``` 41 | var_dump(baht_text('500a')); // NULL 42 | ``` --------------------------------------------------------------------------------