Overview

Namespaces

  • Rundiz
    • Number

Classes

  • Rundiz\Number\Number
  • Rundiz\Number\NumberEng
  • Rundiz\Number\NumberThai
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Number Thai class
  4:  *
  5:  * @package Number
  6:  * @since 1.0
  7:  * @author Vee W.
  8:  * @license http://opensource.org/licenses/MIT
  9:  *
 10:  */
 11: 
 12: namespace Rundiz\Number;
 13: 
 14: /**
 15:  * convert number such as -10, 0.034, 0.50, 1.34, 1000000, or more to text in Thai language.
 16:  * 
 17:  * @author Vee W.
 18:  * @license MIT.
 19:  * @link http://rundiz.com author's home page.
 20:  * @uses to convert number to text, make a call to from this class to convertNumber() method. Example: $number_thai->convertNumber('109.99');
 21:  */
 22: class NumberThai 
 23: {
 24: 
 25: 
 26:     public $number = array('ศูนย์', 'หนึ่ง', 'สอง', 'สาม', 'สี่','ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า');
 27:     public $number_scale = array('', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน');
 28:     
 29:     // arabic and thai number
 30:     public $arabic_number = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
 31:     public $thai_number = array('๐', '๑', '๒', '๓', '๔', '๕', '๖', '๗', '๘', '๙');
 32: 
 33: 
 34:     /**
 35:      * convert from arabic number to thai number.
 36:      * 
 37:      * @param string $input string that contain number to convert
 38:      * @return string return string with converted number.
 39:      */
 40:     public function arabicToThaiNumber($input)
 41:     {
 42:         $input = strval($input);
 43:         
 44:         return str_replace($this->arabic_number, $this->thai_number, $input);
 45:     }// arabicToThaiNumber
 46: 
 47: 
 48:     /**
 49:      * convert Thai Baht number to text.
 50:      * 
 51:      * @param number $num input the money number. negative or positive.
 52:      * @param boolean $display_net display net (ถ้วน). true to display, false to not display.
 53:      * @return string return converted number to Thai Baht and Satang string.
 54:      */
 55:     public function convertBaht($num, $display_net = true)
 56:     {
 57:         // make input as string.
 58:         $num = strval($num);
 59: 
 60:         if (strpos($num, '.') !== false) {
 61:             list($num, $dec) = explode('.', $num);
 62:         } else {
 63:             $dec = 0;
 64:         }
 65: 
 66:         $output = '';
 67: 
 68:         if ($num{0} == '-') {
 69:             $output .= 'ลบ';
 70:             $num = ltrim($num, '-');
 71:         } elseif ($num{0} == '+') {
 72:             $output .= 'บวก';
 73:             $num = ltrim($num, '+');
 74:         }
 75: 
 76:         if ($num{0} == '0') {
 77:             $output .= 'ศูนย์';
 78:         } else {
 79:             $output .= $this->convertNumberWithScale($num);
 80:         }
 81:         $output .= 'บาท';
 82: 
 83:         if ($dec > 0) {
 84:             // if there is decimal (.)
 85:             $dec_str = '';
 86:             
 87:             // convert number normally for decimal.
 88:             $dec_str = $this->convertNumberWithScale($dec);
 89:             
 90:             if ($dec_str != null) {
 91:                 $output .= $dec_str . 'สตางค์';
 92:             }
 93:         }
 94:         
 95:         if (!isset($dec_str) || (isset($dec_str) && $dec_str == null) && $display_net === true) {
 96:             $output .= 'ถ้วน';
 97:         }
 98:         
 99:         unset($dec, $dec_str);
100:         return $output;
101:     }// convertBaht
102: 
103: 
104:     /**
105:      * match number to text.
106:      * 
107:      * @param number $digit only one digit per request.
108:      * @return string return translated number for each digit requested.
109:      */
110:     public function convertDirectNum($digit)
111:     {
112:         if (isset($this->number[$digit])) {
113:             return $this->number[$digit];
114:         }
115:         return $digit;
116:     }// convertDirectNum
117: 
118: 
119:     /**
120:      * convert the number (and with dot).
121:      * 
122:      * @param number $num number integer or decimal. negative or positive.
123:      * @return string translated number to text in Thai language.
124:      */
125:     public function convertNumber($num)
126:     {
127:         // make input as string.
128:         $num = strval($num);
129: 
130:         if (strpos($num, '.') !== false) {
131:             list($num, $dec) = explode('.', $num);
132:         } else {
133:             $dec = 0;
134:         }
135: 
136:         $output = '';
137: 
138:         if ($num{0} == '-') {
139:             $output .= 'ลบ';
140:             $num = ltrim($num, '-');
141:         } elseif ($num{0} == '+') {
142:             $output .= 'บวก';
143:             $num = ltrim($num, '+');
144:         }
145: 
146:         if ($num{0} == '0') {
147:             $output .= 'ศูนย์';
148:         } else {
149:             $output .= $this->convertNumberWithScale($num);
150:         }
151: 
152:         if ($dec > 0) {
153:             // if there is decimal (.)
154:             $output .= 'จุด';
155:             if ($dec{0} == '0') {
156:                 // first digit after dot is zero. read number directly
157:                 for ($i = 0; $i < strlen($dec); $i++) {
158:                     $output .= $this->convertDirectNum($dec{$i});
159:                 }
160:             } else {
161:                 // read number normally.
162:                 $output .= $this->convertNumberWithScale($dec);
163:             }
164:         }
165: 
166:         return $output;
167:     }// convertNumber
168: 
169: 
170:     /**
171:      * convert the number to text with scale. (ten, hundred, thousand, ...) in Thai language.
172:      * 
173:      * @param number $digits number only. no negative or positive sign. no dot.
174:      * @return string
175:      */
176:     private function convertNumberWithScale($digits)
177:     {
178:         $length_digit = strlen($digits);
179:         $count = 1;
180:         $pos = 0;// หลักเลข 1=หน่วย, 2=สิบ, 3=ร้อย, ...
181:         $output = '';
182:         $tmp_output = '';
183:         $tmp_output_scale = '';
184: 
185:         for($i=$length_digit-1; $i > -1 ; --$i) {
186:             if ($pos == 7) {
187:                 $pos = 1;
188:             }
189: 
190:             $tmp_output = $this->convertDirectNum($digits{$i});
191: 
192:             if ($pos >= 0 && $digits{$i} == 0 && $length_digit > $count) {
193:                 // หากหลักมากกว่าหน่วย และตัวเลขที่เจอเป็นศูนย์ ไม่ให้แสดงตัวอักษรคำว่าศูนย์ เพราะไม่อ่านสิบศูนย์ หรือ ร้อยศูนย์ศูนย์
194:                 $tmp_output = '';
195:             } elseif ($pos == 1 && $digits{$i} == 1) {
196:                 // หากเป็นหลักสิบ และตัวเลขที่เจอเป็น 1 ไม่ให้แสดงตัวอักษร คำว่า หนึ่ง เนื่องจากเราจะไม่อ่านว่า หนึ่งสิบ
197:                 $tmp_output = '';
198:             } elseif ($pos == 1 && $digits{$i} == 2) {
199:                 // หน่วยสิบ เลขคือ 2
200:                 $tmp_output = 'ยี่';
201:             } elseif (($pos == 0 || $pos == 6) && $digits{$i} == 1 && $length_digit > $count) {
202:                 // หากเป็นหลักหน่วย หรือหลักล้าน และตัวเลขที่พบคือ 1 และยังมีหลักที่มากกว่าหลักหน่วยปัจจุบัน ให้แสดงเป็น เอ็ด แทน หนึ่ง
203:                 $tmp_output = 'เอ็ด';
204:             }
205: 
206:             if (isset($this->number_scale[$pos])) {
207:                 // generate number scale (สิบ ร้อย พัน ...)
208:                 $tmp_output_scale = $this->number_scale[$pos];
209:             }
210:             if ($digits{$i} == 0 && $pos != 6) {
211:                 // ถ้าตัวเลขที่พบเป็น 0 และไม่ใช่หลักล้าน ไม่ให้แสดงอักษรของหลัก
212:                 $tmp_output_scale = '';
213:             }
214: 
215:             $output = $tmp_output . $tmp_output_scale . $output;
216: 
217:             $count++;
218:             $pos++;
219:             $tmp_output = '';
220:             $tmp_output_scale = '';
221:         }
222: 
223:         unset($count, $i, $length_digit, $pos, $tmp_output, $tmp_output_scale);
224:         return $output;
225:     }// convertNumberWithScale
226: 
227: 
228:     /**
229:      * convert from thai number to arabic number.
230:      * 
231:      * @param string $input input string that contain number to convert.
232:      * @return string return string with converted number
233:      */
234:     public function thaiToArabicNumber($input)
235:     {
236:         $input = strval($input);
237:         
238:         return str_replace($this->thai_number, $this->arabic_number, $input);
239:     }// thaiToArabicNumber
240: 
241: 
242: }
243: 
API documentation generated by ApiGen