Public Function BahtText(ByVal InputCurrency As Double) As String
' check for negative val
If InputCurrency < 0 Then
InputCurrency = -InputCurrency
BahtText = "ลบ"
End If
StrTmp1 = Format(InputCurrency, "0.00") ' rounds up to 2 decimals
InputCurrency = Val(StrTmp1)
IntegerValue = Int(InputCurrency) ' get integer value
DecimalValue = (InputCurrency - IntegerValue) * 100 ' get 2 decimal values
' check for zeto val
If IntegerValue = 0 And DecimalValue = 0 Then
Satang = "ศูนย์บาทถ้วน"
GoTo locExit
End If
' translate integer val to name if necesary
If IntegerValue > 0 Then
StrTmp = Left(StrTmp1, Len(StrTmp1) - 3) ' get string of integer val
StrLen = Len(StrTmp) ' get string len
CurrDigit = 0
' scan integer string and compute its name
For ScanDigit = StrLen To 1 Step -1
' save previous digit
PrevDigit = CurrDigit
' get digit base
DigitBase = ScanDigit Mod 6
' convert digit character to numeric value
CurrDigit = Asc(Mid(StrTmp, StrLen - ScanDigit + 1, 1)) - 48
' get unit name from its base
UnitSave = RTrim(Mid(UnitName, DigitBase * 5 + 1, 5))
' get number name from Currdigit, depends on the digit base
DigitSave = RTrim(Mid(IIf(DigitBase = 2, DigitName1, DigitName), CurrDigit * 5 + 1, 5))
' base ten and number 1
If DigitBase = 1 And CurrDigit = 1 And PrevDigit <> 0 Then
DigitSave = "เอ็ด"
End If
If DigitBase = 2 And CurrDigit = 1 Then
DigitSave = ""
End If
' first digit base may be base million or 1
If DigitBase = 1 And ScanDigit < 6 Then
UnitSave = ""
End If
' ignore add digit name in result string if it is zero
If CurrDigit <> 0 Then
BahtText = BahtText + DigitSave + UnitSave
ElseIf DigitBase = 1 Then
BahtText = BahtText + UnitSave
End If
Next ScanDigit
BahtText = BahtText + "บาท"
End If
' if no decimal value
If DecimalValue = 0 Then
Satang = "ถ้วน"
' compute decimal val to name, there are only 2 digit
Else
StrTmp = Right(StrTmp1, 2)
' name ot first digit
CurrDigit = Asc(Left(StrTmp, 1)) - 48
PrevDigit = CurrDigit
If CurrDigit > 0 Then
If CurrDigit = 1 Then
Satang = "สิบ"
Else
Satang = RTrim(Mid(DigitName1, CurrDigit * 5 + 1, 5)) + "สิบ"
End If
Else
Satang = ""
End If
' name of last digit
CurrDigit = Asc(Right(StrTmp, 1)) - 48
If CurrDigit > 0 Then
Satang = Satang + IIf(CurrDigit = 1 And PrevDigit <> 0, "เอ็ด", RTrim(Mid(DigitName, CurrDigit * 5 + 1, 5)))
End If
' store result and unit
Satang = Satang + "สตางค์"
End If
locExit:
' store result to BahtText
BahtText = BahtText + Satang
End Function