Private suffix() As String = {"", "", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน", "ล้าน"}
Private numSpeak() As String = {"", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า"}
Public Function ThaiBahtText(ByVal m As Double) As String
Dim s1 As String = "" ' ---- ส่วนที่เกินหลักล้านขึ้นไป (ล้าน)
Dim s2 As String = "" ' ---- ส่วนจำนวนเต็ม (บาท)
Dim s3 As String = "" ' ---- ส่วนสตางค์ (สตางค์)
Dim result As New System.Text.StringBuilder
If (m = 0) Then Return ("ศูนย์บาท") ' ---- ศูนย์บาทถ้วน ???
splitCurr(m, s1, s2, s3) ' now 'm' split to 3 parts in 's1' & 's2' & 's3'
If (s1.Length > 0) Then result.Append(Speak(s1) & "ล้าน")
If (s2.Length > 0) Then result.Append(Speak(s2) & "บาท")
If (s3.Length > 0) Then
result.Append(speakStang(s3) & "สตางค์")
Else
result.Append("ถ้วน")
End If
Return (result.ToString)
End Function
Private Function Speak(ByVal s As String) As String
Dim c As Integer
Dim result As New System.Text.StringBuilder
Dim L As Integer
If (s.Length = 0) Then Return ("")
L = s.Length
For i As Integer = 1 To L
If (s.Chars(i - 1) = "-") Then
result.Append("ติดลบ")
Else
c = Val(s.Chars(i - 1))
If ((i = L) And (c = 1)) Then
If (L = 1) Then
Return ("หนึ่ง")
End If
If (L > 1) And (s.Chars(L - 2) = "0") Then
result.Append("หนึ่ง")
Else
result.Append("เอ็ด")
End If
ElseIf ((i = L - 1) And (c = 2)) Then
result.Append("ยี่สิบ")
ElseIf ((i = L - 1) And (c = 1)) Then
result.Append("สิบ")
Else
If (c <> 0) Then
result.Append(numSpeak(c) & suffix(L - i + 1))
End If
End If
End If
Next
Return (result.ToString())
End Function
Private Function speakStang(ByVal s As String) As String
Dim i, L As Integer
Dim c As Integer
Dim result As New System.Text.StringBuilder
L = s.Length
If (L = 0) Then Return ("")
If (L = 1) Then s = s & "0" : L = 2
If (L > 2) Then s = s.Substring(0, 2) : L = 2
For i = 1 To 2
c = Val(s.Chars(i - 1)) ' --- CInt(Mid$(s, i, 1))
If ((i = L) And (c = 1)) Then
If (CInt(Mid$(s, 1, 1)) = 0) Then
result.Append("หนึ่ง")
Else
result.Append("เอ็ด")
End If
ElseIf ((i = L - 1) And (c = 2)) Then
result.Append("ยี่สิบ")
ElseIf ((i = L - 1) And (c = 1)) Then
result.Append("สิบ")
Else
If (c <> 0) Then
result.Append(numSpeak(c) & suffix(2 - i + 1))
End If
End If
Next
Return (result.ToString())
End Function
Private Sub splitCurr(ByVal m As Double, _
ByRef s1 As String, _
ByRef s2 As String, _
ByRef s3 As String)
Dim s As String
Dim L, position As Integer
s = CStr(m)
position = s.IndexOf(".") + 1 ' --- InStr( 1, s, ".")
If (position <> 0) Then
'this currency have a point
s1 = s.Substring(0, position - 1) ' Mid$(s, 1, position - 1)
s3 = s.Substring(position) ' Mid$(s, position + 1, 2)
If s3 = "00" Then s3 = ""
Else
s1 = s
s3 = ""
End If
L = s1.Length
If (L > 6) Then
s2 = s1.Substring(L - 5 - 1) ' --- Mid$(s1, L - 5, 99)
s1 = s1.Substring(0, L - 6) ' Mid$(s1, 1, L - 6)
Else
s2 = s1
s1 = ""
End If
If (Not IsNumeric(s1)) Then s1 = ""
If (Not IsNumeric(s2)) Then s2 = ""
If (Val(s1) = 0) Then s1 = ""
If (Val(s2) = 0) Then s2 = ""
End Sub
End Class