ตัวอักษรขึ้นผิดตอนใช้ BahtText
กระทู้เก่าบอร์ด อ.Yeadram

 1,115   2
URL.หัวข้อ / URL
ตัวอักษรขึ้นผิดตอนใช้ BahtText

รบกวนถามเรื่อง BahtText ค่ะ
ตัวเลขถูกเปลี่ยนเป็นตัวอักษรค่ะแต่ผิด

เช่นใส่ "2,000"
จะออกมาเป็น "ง สองน หมืบาทถ้วน"
ใส่ "25,000"
จะออกมาเป็น "ง สอง่น้า หกน หมืบาทถ้วน"

ไม่รู้จะแก้ยังไงดี

ตอนนี้ใช้โค้ดตัวนี้ค่ะ

Public Function BahtText(InputCurrency As Currency) As String
    Dim DigitSave, UnitSave, DigitName, DigitName1, UnitName, Satang, StrTmp, StrTmp1 As String
    Dim DecimalValue, CurrDigit, PrevDigit, StrLen, DigitBase, ScanDigit As Integer
    Dim IntegerValue As Double

    ' init variable
    DigitName = "ศูนย์ หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า"    ' name of digit number
    DigitName1 = "ยี่ สาม สี่ ห้า หก เจ็ด แปด เก้า"        ' name of digit number in another call
    UnitName = "แสน ล้าน สิบ ร้อย พัน หมื่น"      ' name of digit base
    BahtText = ""
    Satang = ""

    ' 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

            ' 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
            Satang = RTrim(Mid(DigitName1, CurrDigit * 5 + 1, 5)) + "สิบ"
        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

2 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R23003
เหมือนฟังชั่นผิดนะครับ ลองใช้ฟังก์ชั่นนี้แทนนะครับ

Function BahtText(ByVal sNum)
    Dim sNumber , sDigit , sDigit10
    Dim nLen , sWord , sWord2
    Dim sByte , I , J

    sNumber = Array("", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า")
    sDigit = Array("", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน")
    sDigit10 = Array("", "สิบ", "ยี่สิบ", "สามสิบ", "สี่สิบ", "ห้าสิบ", "หกสิบ", "เจ็ดสิบ", "แปดสิบ", "เก้าสิบ")
    sNum = Replace(FormatNumber(sNum, 2), ",", "")
    nLen = Len(sNum)

    If sNum = ".00" Then BahtText = "ศูนย์"
    For I = 1 To nLen - 3
        J = (15 + nLen - I) Mod 6
        sByte = Mid(sNum, I, 1)
        If sByte <> "0" Then
            If J = 1 Then sWord = sDigit10(sByte) Else sWord = sNumber(sByte) & sDigit(J)
            BahtText = BahtText & sWord
        End If
        If J = 0 And I <> nLen - 3 Then
            BahtText = BahtText & "ล้าน": BahtText = Replace(BahtText, "หนึ่งล้าน", "เอ็ดล้าน")
        End if
    Next
    If Left(sNum, 1) = "1" Then BahtText = Replace(BahtText, "เอ็ดล้าน", "หนึ่งล้าน")
    If Left(sNum, 2) = "11" Then BahtText = Replace(BahtText, "สิบหนึ่งล้าน", "สิบเอ็ดล้าน")
    If Len(BahtText) > 0 Then BahtText = BahtText & "บาท"
    If nLen > 4 Then BahtText = Replace(BahtText, "หนึ่งบาท", "เอ็ดบาท")
    sNum = Right(sNum, 2)
    If sNum = "00" Then
        BahtText = BahtText & "ถ้วน"
    Else
        If Left(sNum, 1) <> "0" Then BahtText = BahtText & sDigit10(Left(sNum, 1))
        If Right(sNum, 1) <> "0" Then BahtText = BahtText & sNumber(Right(sNum, 1))
        BahtText = BahtText & "สตางค์"
        If Left(sNum, 1) <> "0" Then BahtText = Replace(BahtText, "หนึ่งสตางค์", "เอ็ดสตางค์")
    End If
End Function
2 @R23005
ใช้ได้แล้วค่ะ ขอบคุณนะคะ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.2672s