55Adapted from 
66http://justmindthought.blogspot.com/2012/12/code-php.html 
77""" 
8- import  ast 
98import  math 
109
1110__all__  =  ["bahttext" , "num_to_thaiword" ]
1211
13- p  =  [
12+ _p  =  [
1413    ["ภาษาไทย" , "ตัวเลข" , "เลขไทย" ],
1514    ["หนึ่ง" , "1" , "๑" ],
1615    ["สอง" , "2" , "๒" ],
2221    ["แปด" , "8" , "๘" ],
2322    ["เก้า" , "9" , "๙" ],
2423]
25- thaitonum  =  dict ((x [2 ], x [1 ]) for  x  in  p [1 :])
26- p1  =  dict ((x [0 ], x [1 ]) for  x  in  p [1 :])
27- d1  =  0 
2824
2925
30- # เลขไทยสู่เลข  
26+ # เลขไทยสู่เลขอารบิก  
3127def  thai_num_to_num (text ):
3228    """ 
3329    :param str text: Thai number characters such as '๑', '๒', '๓' 
3430    :return: universal numbers such as '1', '2', '3' 
3531    """ 
36-     thaitonum  =  dict ((x [2 ], x [1 ]) for  x  in  p [1 :])
32+     thaitonum  =  dict ((x [2 ], x [1 ]) for  x  in  _p [1 :])
3733    return  thaitonum [text ]
3834
3935
@@ -42,7 +38,7 @@ def thai_num_to_text(text):
4238    :param str text: Thai number characters such as '๑', '๒', '๓' 
4339    :return: Thai numbers, spelled out in Thai 
4440    """ 
45-     thaitonum  =  dict ((x [2 ], x [0 ]) for  x  in  p [1 :])
41+     thaitonum  =  dict ((x [2 ], x [0 ]) for  x  in  _p [1 :])
4642    return  thaitonum [text ]
4743
4844
@@ -51,7 +47,7 @@ def num_to_thai_num(text):
5147    :param text: universal numbers such as '1', '2', '3' 
5248    :return: Thai number characters such as '๑', '๒', '๓' 
5349    """ 
54-     thaitonum  =  dict ((x [1 ], x [2 ]) for  x  in  p [1 :])
50+     thaitonum  =  dict ((x [1 ], x [2 ]) for  x  in  _p [1 :])
5551    return  thaitonum [text ]
5652
5753
@@ -60,7 +56,7 @@ def num_to_text(text):
6056    :param text: universal numbers such as '1', '2', '3' 
6157    :return: Thai numbers, spelled out in Thai 
6258    """ 
63-     thaitonum  =  dict ((x [1 ], x [0 ]) for  x  in  p [1 :])
59+     thaitonum  =  dict ((x [1 ], x [0 ]) for  x  in  _p [1 :])
6460    return  thaitonum [text ]
6561
6662
@@ -69,7 +65,7 @@ def text_to_num(text):
6965    :param text: Thai numbers, spelled out in Thai 
7066    :return: universal numbers such as '1', '2', '3' 
7167    """ 
72-     thaitonum  =  dict ((x [0 ], x [1 ]) for  x  in  p [1 :])
68+     thaitonum  =  dict ((x [0 ], x [1 ]) for  x  in  _p [1 :])
7369    return  thaitonum [text ]
7470
7571
@@ -78,47 +74,34 @@ def text_to_thai_num(text):
7874    :param text: Thai numbers, spelled out in Thai 
7975    :return: Thai numbers such as '๑', '๒', '๓' 
8076    """ 
81-     thaitonum  =  dict ((x [0 ], x [2 ]) for  x  in  p [1 :])
77+     thaitonum  =  dict ((x [0 ], x [2 ]) for  x  in  _p [1 :])
8278    return  thaitonum [text ]
8379
8480
85- def  number_format (num , places = 0 ):
86-     return  "{:20,.2f}" .format (num )
87- 
88- 
89- def  bahttext (amount_number ):
81+ def  bahttext (number ):
9082    """ 
9183    Converts a number to Thai text and adds a suffix of "Baht" currency. 
84+     Precision will be fixed at two decimal places (0.00) to fits "Satang" unit. 
9285
9386    Similar to BAHTTEXT function in Excel 
9487    """ 
9588    ret  =  "" 
9689
97-     if  amount_number  is  None :
90+     if  number  is  None :
9891        pass 
99-     elif  amount_number  ==  0 :
92+     elif  number  ==  0 :
10093        ret  =  "ศูนย์บาทถ้วน" 
10194    else :
102-         amount_number  =  number_format (amount_number , 2 ).replace (" " , "" )
103-         pt  =  amount_number .find ("." )
104-         number , fraction  =  "" , "" 
105-         amount_number1  =  amount_number .split ("." )
106- 
107-         if  not  pt :
108-             number  =  amount_number 
109-         else :
110-             amount_number  =  amount_number .split ("." )
111-             number  =  amount_number [0 ]
112-             fraction  =  int (amount_number1 [1 ])
113- 
114-         number  =  ast .literal_eval (number .replace ("," , "" ))
95+         num_int , num_dec  =  "{:.2f}" .format (number ).split ("." )
96+         num_int  =  int (num_int )
97+         num_dec  =  int (num_dec )
11598
116-         baht  =  num_to_thaiword (number )
117-         if  baht   !=   "" :
99+         baht  =  num_to_thaiword (num_int )
100+         if  baht :
118101            ret  =  "" .join ([ret , baht , "บาท" ])
119102
120-         satang  =  num_to_thaiword (fraction )
121-         if  satang  !=   ""   and  satang  !=  "ศูนย์" :
103+         satang  =  num_to_thaiword (num_dec )
104+         if  satang  and  satang  !=  "ศูนย์" :
122105            ret  =  "" .join ([ret , satang , "สตางค์" ])
123106        else :
124107            ret  =  "" .join ([ret , "ถ้วน" ])
@@ -139,7 +122,18 @@ def num_to_thaiword(number):
139122        ret  =  "ศูนย์" 
140123    else :
141124        _POS_CALL  =  ["แสน" , "หมื่น" , "พัน" , "ร้อย" , "สิบ" , "" ]
142-         _NUM_CALL  =  ["" , "หนึ่ง" , "สอง" , "สาม" , "สี่" , "ห้า" , "หก" , "เจ็ด" , "แปด" , "เก้า" ]
125+         _NUM_CALL  =  [
126+             "" ,
127+             "หนึ่ง" ,
128+             "สอง" ,
129+             "สาม" ,
130+             "สี่" ,
131+             "ห้า" ,
132+             "หก" ,
133+             "เจ็ด" ,
134+             "แปด" ,
135+             "เก้า" ,
136+         ]
143137
144138        if  number  >  1000000 :
145139            ret  +=  num_to_thaiword (int (number  /  1000000 )) +  "ล้าน" 
0 commit comments