ฟังชั่น Bahttext

Post date: Aug 23, 2013 2:38:59 AM

แปลงจำนวนเงินบาทที่เป็นตัวเลข เป็นตัวอักษรไทย

Function toBahttext(num As String) As String

Dim lenMax As Integer, B As Byte, Satang As String, Baht As String, i As Integer, max As Integer, r As String, n As String, formatnum As String, result As String, BSloop As Integer, BS As String

formatnum = Format(num, “######0.00″)

lenMax = Len(formatnum)

Satang = Right(formatnum, 2)

Baht = Left(formatnum, lenMax – 3)

If Baht <> “0″ Then

BSloop = 1

ElseIf Satang <> “00″ Then

BSloop = 2

Else

BSloop = 4

End If

Do While BSloop < 3

BS = Choose(BSloop, Baht, Satang)

max = Len(BS)

For i = 1 To max

r = Choose(((max – i + 1) Mod 6) + 1, “แสน”, “”, “สิบ”, “ร้อย”, “พัน”, “หมื่น”)

n = Choose(Mid(BS, i, 1) + 1, “ศูนย์”, “หนึ่ง”, “สอง”, “สาม”, “สี่”, “ห้า”, “หก”, “เจ็ด”, “แปด”, “เก้า”)

If r = “สิบ” And n = “หนึ่ง” Then n = “”

If n = “หนึ่ง” And r = “” And max <> 1 Then n = “เอ็ด”

If i = 1 And n = “เอ็ด” And max > 1 Then n = “หนึ่ง”

If r = “สิบ” And n = “สอง” Then n = “ยี่”

If r = “” And max – i + 1 > 6 Then r = “ล้าน”

If n <> “ศูนย์” Then

result = result + n + r

Else

If r = “ล้าน” Then result = result + r

End If

Next i

If BSloop = 1 Then result = result + “บาท”

If BSloop = 1 And Satang = “00″ Then

result = result + “ถ้วน”

Exit Do

End If

If BSloop = 2 Then

result = result + “สตางค์”

Exit Do

End If

BSloop = BSloop + 1

Loop

toBahttext = “***” & result & “***”

End Function

หรือ

Function NumToString(Num As String)

Dim numstring(10) As String

Dim million As Integer

Dim hundred_million As Integer

Dim ten_thousand As Integer

Dim thousand As Integer

Dim hundred As Integer

Dim ten As Integer

Dim onebath As Integer

Dim inputstring As String

Dim numLen As Integer

Dim a As String

Dim result As String

Dim bTen As Boolean

numstring(1) = “ศูนย์”

numstring(2) = “หนึ่ง”

numstring(3) = “สอง”

numstring(4) = “สาม”

numstring(5) = “สี่”

numstring(6) = “ห้า”

numstring(7) = “หก”

numstring(8) = “เจ็ด”

numstring(9) = “แปด”

numstring(10) = “เก้า”

inputstring = Format$(Num, “######0.00″)

numLen = Len(inputstring)

If numLen >= 10 Then

a = Left$(inputstring, numLen – 9)

If Len(a) = 1 Then result = numstring(Val(a) + 1) + “ล้าน”

End If

If numLen >= 9 Then

a = Mid$(inputstring, numLen – 8, 1)

result = result + numstring(Val(a) + 1) + “แสน”

End If

If numLen >= 8 Then

a = Mid$(inputstring, numLen – 7, 1)

If Val(a) > 0 Then result = result + numstring(Val(a) + 1) + “หมื่น”

End If

If numLen >= 7 Then

a = Mid$(inputstring, numLen – 6, 1)

If Val(a) > 0 Then result = numstring(Val(a) + 1) + “พัน”

End If

If numLen >= 6 Then

a = Mid$(inputstring, numLen – 5, 1)

If Val(a) > 0 Then result = result + numstring(Val(a) + 1) + “ร้อย”

End If

If numLen >= 5 Then

bTen = True

a = Mid$(inputstring, numLen – 4, 1)

If Val(a) = 0 Then bTen = False

If Val(a) = 1 Then result = result + “สิบ”

If Val(a) = 2 Then result = result + “ยี่สิบ”

If Val(a) >= 3 Then result = result + numstring(Val(a) + 1) + “สิบ”

End If

If numLen >= 4 Then

a = Mid$(inputstring, numLen – 3, 1)

If Val(a) = 0 And Len(result) = 0 And Right$(inputstring, 2) = “00″ Then result = “ศูนย์”

If Val(a) = 1 Then

If bTen = True Then result = result + “เอ็ด”

If bTen = False Then result = result + “หนึ่ง”

End If

If Val(a) >= 2 Then result = result + numstring(Val(a) + 1)

End If

a = Right$(inputstring, 2)

If a = “00″ Then

result = result + “บาทถ้วน”

Else

bTen = True

If Len(result) > 0 Then result = result + “บาท”

If Val(Left$(a, 1)) = 0 Then bTen = False

If Val(Left$(a, 1)) = 1 Then result = result + “สิบ”

If Val(Left$(a, 1)) = 2 Then result = result + “ยี่สิบ”

If Val(Left$(a, 1)) >= 3 Then result = result + numstring(Val(Left$(a, 1)) + 1) + “สิบ”

If Val(Right$(a, 1)) = 1 Then

If bTen = True Then result = result + “เอ็ด”

If bTen = False Then result = result + “หนึ่ง”

End If

If Val(Right$(a, 1)) >= 2 Then result = result + numstring(Val(Right$(a, 1)) + 1)

result = result + “สตางค์”

End If

NumToString = result

End Function