doc so trong access
Bạn chép cả 3 hàm này vào rồi gọi DocSo(n) là được
'C¸c hµm dïng ®äc sè
'==================================================
Function DocSo(X As String) As String
Dim DonVi, Am As Boolean
DonVi = Array("", "ngh×n ", "triÖu ", "tû ")
Dim So As String, Chuoi As String, Temp As String, X1 As String, c As Byte, l As Byte, k As Byte, ChuoiDem As String
Dim id As Byte
X = Format(val(X), "#"): Am = False
If Len(X) > 18 Then
DocSo = "Sè qu¸ lín"
Exit Function
End If
If Left(X, 1) = "-" Then
Am = True
X = Right(X, Len(X) - 1)
End If
If X = 0 Then
DocSo = "Kh«ng"
Exit Function
End If
'Xu ly doc nhung so >100 ty
l = Len(X)
c = Fix(l / 9)
If l Mod 9 = 0 Then
k = 9
Else
k = l Mod 9
End If
X1 = Left(X, k)
X = Right(X, l - k)
Do Until X1 = ""
id = 0
Do While (X1 <> "")
If Len(X1) <> 0 Then
So = Lay3so(X1)
X1 = Left(X1, Len(X1) - Len(So))
Temp = Tinh3so(So)
So = Temp
If So <> "" Then
Temp = Temp + DonVi(id)
Chuoi = Temp + Chuoi
End If
id = id + 1
End If
Loop
l = Len(X)
c = Fix(l)
If (l <> 0) And (l Mod 9) = 0 Then
k = 9
Else
k = l Mod 9
End If
X1 = Left(X, k)
X = Right(X, l - k)
ChuoiDem = ChuoiDem & Chuoi
Chuoi = ""
If X = "" And X1 <> "" Then ChuoiDem = ChuoiDem & "tû "
Loop
ChuoiDem = IIf(Am, "¢m " & Trim$(ChuoiDem), UCase(Left(ChuoiDem, 1)) & Right(ChuoiDem, Len(ChuoiDem) - 1))
DocSo = ChuoiDem
End Function
Function Lay3so(X As String) As String
Dim So As String
If Len(X) >= 3 Then
So = Right(X, 3)
Else
So = Right(X, Len(X))
End If
Lay3so = So
End Function
Function Tinh3so(X As String) As String
Dim Chuoi As String, Temp As String
Dim Flag0 As Boolean, Flag1 As Boolean
Temp = X
Dim KySo
KySo = Array("kh«ng", "mét", "hai", "ba", "bèn", "n¨m", "s¸u", "b¶y", "t¸m", "chÝn")
If Len(X) = 3 Then
If X <> "000" Then 'If Left(x, 1) <> 0 Then
Chuoi = KySo(Left(X, 1)) & " tr¨m "
End If
X = Right(X, 2)
End If
If Len(X) = 2 Then
If Left(X, 1) = 0 Then
If Right(X, 1) <> 0 Then
Chuoi = Chuoi & "linh "
End If
Flag0 = True
Else
If Left(X, 1) = 1 Then
Chuoi = Chuoi & "mêi "
Else
Chuoi = Chuoi & KySo(Left(X, 1)) & " m¬i "
Flag1 = True
End If
End If
X = Right(X, 1)
End If
If Right(X, 1) <> "0" Then
If Left(X, 1) = "5" And Not Flag0 Then
If Len(Temp) = 1 Then
Chuoi = Chuoi & "n¨m "
Else
Chuoi = Chuoi & "l¨m "
End If
Else
If Left(X, 1) = "1" And Not (Not Flag1 Or Flag0) And Chuoi <> "" Then
Chuoi = Chuoi & "mèt "
Else
Chuoi = Chuoi & KySo(Left(X, 1)) & " "
End If
End If
End If
Tinh3so = Chuoi
End Function