Created
June 7, 2024 19:38
-
-
Save phattarachai/b3ca8bee99983fe05945de63de89dff1 to your computer and use it in GitHub Desktop.
PHP Bath Text ตัวเลขเงินบาทภาษาไทย
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function baht_text($number, $include_unit = true, $display_zero = true): ?string | |
| { | |
| if (!is_numeric($number)) { | |
| return null; | |
| } | |
| $BAHT_TEXT_NUMBERS = ['ศูนย์', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า']; | |
| $BAHT_TEXT_UNITS = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']; | |
| $BAHT_TEXT_ONE_IN_TENTH = 'เอ็ด'; | |
| $BAHT_TEXT_TWENTY = 'ยี่'; | |
| $BAHT_TEXT_INTEGER = 'ถ้วน'; | |
| $BAHT_TEXT_BAHT = 'บาท'; | |
| $BAHT_TEXT_SATANG = 'สตางค์'; | |
| $BAHT_TEXT_POINT = 'จุด'; | |
| $log = floor(log($number, 10)); | |
| if ($log > 5) { | |
| $millions = floor($log / 6); | |
| $million_value = pow(1000000, $millions); | |
| $normalised_million = floor($number / $million_value); | |
| $rest = $number - ($normalised_million * $million_value); | |
| $millions_text = str_repeat($BAHT_TEXT_UNITS[6], $millions); | |
| return baht_text($normalised_million, false) . $millions_text . baht_text($rest, true, false); | |
| } | |
| $number_str = (string)floor($number); | |
| $text = ''; | |
| $unit = 0; | |
| if ($display_zero && $number_str == '0') { | |
| $text = $BAHT_TEXT_NUMBERS[0]; | |
| } else { | |
| for ($i = strlen($number_str) - 1; $i > -1; $i--) { | |
| $current_number = (int)$number_str[$i]; | |
| $unit_text = ''; | |
| if ($unit == 0 && $i > 0) { | |
| $previous_number = isset($number_str[$i - 1]) ? (int)$number_str[$i - 1] : 0; | |
| if ($current_number == 1 && $previous_number > 0) { | |
| $unit_text .= $BAHT_TEXT_ONE_IN_TENTH; | |
| } else { | |
| if ($current_number > 0) { | |
| $unit_text .= $BAHT_TEXT_NUMBERS[$current_number]; | |
| } | |
| } | |
| } else { | |
| if ($unit == 1 && $current_number == 2) { | |
| $unit_text .= $BAHT_TEXT_TWENTY; | |
| } else { | |
| if ($current_number > 0 && ($unit != 1 || $current_number != 1)) { | |
| $unit_text .= $BAHT_TEXT_NUMBERS[$current_number]; | |
| } | |
| } | |
| } | |
| if ($current_number > 0) { | |
| $unit_text .= $BAHT_TEXT_UNITS[$unit]; | |
| } | |
| $text = $unit_text . $text; | |
| $unit++; | |
| } | |
| } | |
| if ($include_unit) { | |
| $text .= $BAHT_TEXT_BAHT; | |
| $satang = explode('.', number_format($number, 2, '.', ''))[1]; | |
| $text .= $satang == 0 | |
| ? $BAHT_TEXT_INTEGER | |
| : baht_text($satang, false) . $BAHT_TEXT_SATANG; | |
| } else { | |
| $exploded = explode('.', $number); | |
| if (isset($exploded[1])) { | |
| $text .= $BAHT_TEXT_POINT; | |
| $decimal = $exploded[1]; | |
| for ($i = 0; $i < strlen($decimal); $i++) { | |
| $text .= $BAHT_TEXT_NUMBERS[$decimal[$i]]; | |
| } | |
| } | |
| } | |
| return $text; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment