Tuesday, May 29, 2012

Fungsi Terbilang Bagaimanakah Membuatnya - Database VB6

Posting yang menjelaskan tentang cara membuat fungsi terbilang - Fungsi terbilang adalah sebuah fungsi yang dapat mengkonversi angka ke dalam kalimat. Sebuah fungsi yang cukup penting, terutama pada saat kita bekerja dengan database. Bagaimanakah cara membuat fungsi terbilang ini:
Option Explicit 

Public Function
Terbilang(x As Double, Optional w = "terlalu besar") As String

Dim t As Double, s As String, b As String, i As Integer, d As Boolean,
letak()
letak = Array("", "ribu ", "juta ", "milyar ", "trilyun ")

If
(x = 0) Then
Terbilang = "nol"
Exit Function
End If

If
(x < 2000) Then d = True

If
(x >= 1E+15) Then
Terbilang = w
Exit Function
End If

For i =
4 To 1 Step -1
t = Int(x / (10 ^ (3 * i)))
If (t > 0) Then
b =
ratusan(t, d)
s = s & b & letak(i)
End If
x = x - t *
(10 ^ (3 * i))
Next

s = s
& ratusan(x, False)
Terbilang = s

End Function

Private Function
ratusan(ByVal y As Double, ByVal f As Boolean) As String
Dim t As Double, b As String, g As String, j As Integer,
a(), p()
a = Array("", "se", "dua ", "tiga ", "empat ", "lima ", "enam ", "tujuh ", "delapan ", "sembilan ")
p = Array("", "puluh ", "ratus ")

For j =
2 To 1 Step -1
t = Int(y / (10 ^ j))
If (t > 0) Then
g =
a(t)
If (j = 1 And t = 1) Then
y = y - t *
10 ^ j
If
(y >= 1) Then
p(j) = "belas "
Else
a(y) = "se"
End If
b = b
& a(y) & p(j)
ratusan = b
Exit Function
Else
b = b
& g & p(j)
End If
End If
y = y - t *
10 ^ j
Next

If
(f = False) Then a(1) = "satu "

b = b
& a(y)
ratusan = b

End Function

Contoh penggunaan fungsi di atas:
Option Explicit 

Private Sub
cmdTerbilang_Click()
txtTerbilang.Text = UCase(Terbilang(Val(txtAngka.Text)))
End Sub