Membuat Angka Terbilang dengan Bahasa Inggris dengan Kode VB6

Dibawah ini adalah kode untuk mengkonversi angka menjadi terbilang dengan bahasa inggris menggunakan kode vb6.


Output

Dengan Gambar diatas Anda cukup menambahkan Textbox dengan Name txtAngka dan sebuah Label dengan Name lblTerbilang pada Form1 Anda

Private Function SpellDigit(strNumeric As Integer)

 Dim cRet As String

 On Error GoTo Pesan

 cRet = ""

 Select Case strNumeric

        Case 0:     cRet = " Zero"

        Case 1:     cRet = " One"

        Case 2:     cRet = " Two"

        Case 3:     cRet = " Three"

        Case 4:     cRet = " Four"

        Case 5:     cRet = " Five"

        Case 6:     cRet = " Six"

        Case 7:     cRet = " Seven"

        Case 8:     cRet = " Eight"

        Case 9:     cRet = " Nine"

        Case 10:    cRet = " Ten"

        Case 11:    cRet = " Eleven"

        Case 12:    cRet = " Twelve"

        Case 13:    cRet = " Thirteen"

        Case 14:    cRet = " Fourteen"

        Case 15:    cRet = " Fifteen"

        Case 16:    cRet = " Sixteen"

        Case 17:    cRet = " Seventeen"

        Case 18:    cRet = " Eighteen"

        Case 19:    cRet = " Ninetieen"

        Case 20:    cRet = " Twenty"

        Case 30:    cRet = " Thirty"

        Case 40:    cRet = " Fourthy"

        Case 50:    cRet = " Fifty"

        Case 60:    cRet = " Sixty"

        Case 70:    cRet = " Seventy"

        Case 80:    cRet = " Eighty"

        Case 90:    cRet = " Ninety"

        Case 100:   cRet = " One Hundred"

        Case 200:   cRet = " Two Hundred"

        Case 300:   cRet = " Three Hundred"

        Case 400:   cRet = " Four Hundred"

        Case 500:   cRet = " Five Hundred"

        Case 600:   cRet = " Six hundred"

        Case 700:   cRet = " Seven hundred"

        Case 800:   cRet = " Eight hundred"

        Case 900:   cRet = " Nine hundred"

 End Select

 SpellDigit = cRet

Exit Function

Pesan:

  SpellDigit = "(maksimal 9 digit)"

End Function


Private Function SpellUnit(strNumeric As Integer)

 Dim cRet As String

 Dim n100 As Integer

 Dim n10 As Integer

 Dim n1 As Integer

 On Error GoTo Pesan

 cRet = ""

 n100 = Int(strNumeric / 100) * 100

 n10 = Int((strNumeric - n100) / 10) * 10

 n1 = (strNumeric - n100 - n10)

 If n100 > 0 Then

    cRet = SpellDigit(n100)

 End If

 If n10 > 0 Then

    If n10 = 10 Then

       cRet = cRet & SpellDigit(n10 + n1)

    Else

       cRet = cRet & SpellDigit(n10)

    End If

 End If

 If n1 > 0 And n10 <> 10 Then

    cRet = cRet & SpellDigit(n1)

 End If

 SpellUnit = cRet

 Exit Function

Pesan:

  SpellUnit = "(maksimal 9 digit)"

End Function


Public Function TerbilangInggris(strNumeric As String) As String

 Dim cRet As String

 Dim n1000000 As Long

 Dim n1000 As Long

 Dim n1 As Integer

 Dim n0 As Integer

   On Error GoTo Pesan

   Dim strValid As String, huruf As String * 1

   Dim i As Integer

   strValid = "1234567890.,"

   For i% = 1 To Len(strNumeric)

     huruf = Chr(Asc(Mid(strNumeric, i%, 1)))

     If InStr(strValid, huruf) = 0 Then

       MsgBox "Harus karakter angka!", _

              vbCritical, "Karakter Tidak Valid"

       Exit Function

     End If

   Next i%

 

 If strNumeric = "" Then Exit Function

 If Len(Trim(strNumeric)) > 9 Then GoTo Pesan

 

 cRet = ""

 n1000000 = Int(strNumeric / 1000000) * 1000000

 n1000 = Int((strNumeric - n1000000) / 1000) * 1000

 n1 = Int(strNumeric - n1000000 - n1000)

 n0 = (strNumeric - n1000000 - n1000 - n1) * 100

 If n1000000 > 0 Then

    cRet = SpellUnit(n1000000 / 1000000) & " Million"

 End If

 If n1000 > 0 Then

    cRet = cRet & SpellUnit(n1000 / 1000) & " Thousand"

 End If

 If n1 > 0 Then

    cRet = cRet & SpellUnit(n1)

 End If

 If n0 > 0 Then

    cRet = cRet & " and cents" & SpellUnit(n0)

 End If

 TerbilangInggris = cRet

 Exit Function

Pesan:

  TerbilangInggris = "(maximum 9 digit)"

End Function


Private Sub txtAngka_Change()

   lblTerbilang.Caption = TerbilangInggris(txtAngka.Text)

End Sub

Selamat mencoba semoga berhasil

No comments: