21
21
d1 = 0
22
22
#เลขไทยสู่เลข
23
23
def thai_num_to_num (text ):
24
- """รับค่า ''str'' คืนค่า ''str'' เป็นเลขไทยสู่เลข"""
25
- thaitonum = dict ((x [2 ], x [1 ]) for x in p [1 :])
26
- return thaitonum [text ]
27
- #เลขไทยสู่ข้อความ
24
+ """
25
+ :param str text: Thai number characters such as '๑', '๒', '๓'
26
+ :return: universal numbers such as '1', '2', '3'
27
+ """
28
+ thaitonum = dict ((x [2 ], x [1 ]) for x in p [1 :])
29
+ return thaitonum [text ]
30
+
28
31
def thai_num_to_text (text ):
29
- """รับค่า ''str'' คืนค่า ''str'' เป็นเลขไทยสู่ข้อความ"""
30
- thaitonum = dict ((x [2 ], x [0 ]) for x in p [1 :])
31
- return thaitonum [text ]
32
- #เลขสู่เลขไทย
32
+ """
33
+ :param str text: Thai number characters such as '๑', '๒', '๓'
34
+ :return: Thai numbers, spelled out in Thai
35
+ """
36
+ thaitonum = dict ((x [2 ], x [0 ]) for x in p [1 :])
37
+ return thaitonum [text ]
38
+
33
39
def num_to_thai_num (text ):
34
- """รับค่า ''str'' คืนค่า ''str'' เป็นเลขสู่เลขไทย"""
35
- thaitonum = dict ((x [1 ], x [2 ]) for x in p [1 :])
36
- return thaitonum [text ]
37
- #เลขสู่ข้อความ
40
+ """
41
+ :param text: universal numbers such as '1', '2', '3'
42
+ :return: Thai number characters such as '๑', '๒', '๓'
43
+ """
44
+ thaitonum = dict ((x [1 ], x [2 ]) for x in p [1 :])
45
+ return thaitonum [text ]
46
+
38
47
def num_to_text (text ):
39
- """รับค่า ''str'' คืนค่า ''str'' เป็นเลขสู่ข้อความ"""
40
- thaitonum = dict ((x [1 ], x [0 ]) for x in p [1 :])
41
- return thaitonum [text ]
42
- #ข้อความสู่เลข
48
+ """
49
+ :param text: universal numbers such as '1', '2', '3'
50
+ :return: Thai numbers, spelled out in Thai
51
+ """
52
+ thaitonum = dict ((x [1 ], x [0 ]) for x in p [1 :])
53
+ return thaitonum [text ]
54
+
43
55
def text_to_num (text ):
44
- """รับค่า ''str'' คืนค่า ''str'' เป็นข้อความสู่เลข"""
45
- thaitonum = dict ((x [0 ], x [1 ]) for x in p [1 :])
46
- return thaitonum [text ]
47
- #ข้อความสู่เลขไทย
56
+ """
57
+ :param text: Thai numbers, spelled out in Thai
58
+ :return: universal numbers such as '1', '2', '3'
59
+ """
60
+ thaitonum = dict ((x [0 ], x [1 ]) for x in p [1 :])
61
+ return thaitonum [text ]
62
+
48
63
def text_to_thai_num (text ):
49
- """รับค่า ''str'' คืนค่า ''str'' เป็นข้อความสู่เลขไทย"""
50
- thaitonum = dict ((x [0 ], x [2 ]) for x in p [1 :])
51
- return thaitonum [text ]
64
+ """
65
+ :param text: Thai numbers, spelled out in Thai
66
+ :return: Thai numbers such as '๑', '๒', '๓'
67
+ """
68
+ thaitonum = dict ((x [0 ], x [2 ]) for x in p [1 :])
69
+ return thaitonum [text ]
70
+
52
71
def number_format (num , places = 0 ):
53
72
return '{:20,.2f}' .format (num )
54
73
# fork by http://justmindthought.blogspot.com/2012/12/code-php.html
74
+
55
75
def numtowords (amount_number ):
56
- amount_number = number_format (amount_number , 2 ).replace (" " ,"" )
57
- pt = amount_number .find ("." )
58
- number ,fraction = "" ,""
59
- amount_number1 = amount_number .split ('.' )
60
- if (pt == False ):
61
- number = amount_number
62
- else :
63
- amount_number = amount_number .split ('.' )
64
- number = amount_number [0 ]
65
- fraction = int (amount_number1 [1 ])
66
- ret = ""
67
- number = ast .literal_eval (number .replace ("," ,"" ))
68
- baht = ReadNumber (number )
69
- if (baht != "" ):
70
- ret += baht + "บาท"
71
- satang = ReadNumber (fraction )
72
- if (satang != "" ):
73
- ret += satang + "สตางค์"
74
- else :
75
- ret += "ถ้วน"
76
- return ret
76
+ amount_number = number_format (amount_number , 2 ).replace (" " ,"" )
77
+ pt = amount_number .find ("." )
78
+ number ,fraction = "" ,""
79
+ amount_number1 = amount_number .split ('.' )
80
+ if (pt == False ):
81
+ number = amount_number
82
+ else :
83
+ amount_number = amount_number .split ('.' )
84
+ number = amount_number [0 ]
85
+ fraction = int (amount_number1 [1 ])
86
+ ret = ""
87
+ number = ast .literal_eval (number .replace ("," ,"" ))
88
+ baht = ReadNumber (number )
89
+ if (baht != "" ):
90
+ ret += baht + "บาท"
91
+ satang = ReadNumber (fraction )
92
+ if (satang != "" ):
93
+ ret += satang + "สตางค์"
94
+ else :
95
+ ret += "ถ้วน"
96
+ return ret
97
+
98
+ def readnumber (number ):
99
+ """
100
+ :param float number: a float number (with decimals) indicating a quantity
101
+ :return: a text that indicates the full amount in word form, properly ending each digit with the right term.
102
+ """
103
+ position_call = ["แสน" , "หมื่น" , "พัน" , "ร้อย" , "สิบ" , "" ]
104
+ number_call = ["" , "หนึ่ง" , "สอง" , "สาม" ,"สี่" , "ห้า" , "หก" , "เจ็ด" , "แปด" , "เก้า" ]
105
+ number = number
106
+ ret = ""
107
+ if (number == 0 ): return ret
108
+ if (number > 1000000 ):
109
+ ret += ReadNumber (int (number / 1000000 )) + "ล้าน"
110
+ number = int (math .fmod (number , 1000000 ))
111
+ divider = 100000
112
+ pos = 0
113
+ while (number > 0 ):
114
+ d = int (number / divider )
115
+ if (divider == 10 ) and (d == 2 ):
116
+ ret += "ยี่"
117
+ elif (divider == 10 ) and (d == 1 ):
118
+ ret += ""
119
+ elif ((divider == 1 ) and (d == 1 ) and (ret != "" )):
120
+ ret += "เอ็ด"
121
+ else :
122
+ ret += number_call [d ]
123
+ if d :
124
+ ret += position_call [pos ]
125
+ else :
126
+ ret += ""
127
+ number = number % divider
128
+ divider = divider / 10
129
+ pos += 1
130
+ return ret
77
131
78
- #อ่านจำนวนตัวเลขภาษาไทย
79
- def ReadNumber (number ):
80
- """อ่านจำนวนตัวเลขภาษาไทย รับค่าเป็น ''float'' คืนค่าเป็น ''str''"""
81
- position_call = ["แสน" , "หมื่น" , "พัน" , "ร้อย" , "สิบ" , "" ]
82
- number_call = ["" , "หนึ่ง" , "สอง" , "สาม" ,"สี่" , "ห้า" , "หก" , "เจ็ด" , "แปด" , "เก้า" ]
83
- number = number
84
- ret = ""
85
- if (number == 0 ): return ret
86
- if (number > 1000000 ):
87
- ret += ReadNumber (int (number / 1000000 )) + "ล้าน"
88
- number = int (math .fmod (number , 1000000 ))
89
- divider = 100000
90
- pos = 0
91
- while (number > 0 ):
92
- d = int (number / divider )
93
- if (divider == 10 ) and (d == 2 ):
94
- ret += "ยี่"
95
- elif (divider == 10 ) and (d == 1 ):
96
- ret += ""
97
- elif ((divider == 1 ) and (d == 1 ) and (ret != "" )):
98
- ret += "เอ็ด"
99
- else :
100
- ret += number_call [d ]
101
- if d :
102
- ret += position_call [pos ]
103
- else :
104
- ret += ""
105
- number = number % divider
106
- divider = divider / 10
107
- pos += 1
108
- return ret
109
132
if __name__ == "__main__" :
110
- print (numtowords (4000.0 ))
133
+ print (numtowords (4000.0 ))
0 commit comments