đọc số sang chữ

  • Thread starter Thread starter minghng
  • Ngày gửi Ngày gửi
Liên hệ QC

minghng

Thành viên mới
Tham gia
1/8/08
Bài viết
46
Được thích
3
vui long cho mình hỏi, vì sao code đọc từ số sang chữ thường không đọc được chữ "lẻ", viết thêm vào cũng không được. vd; số 102 thì dịch ra là " một trăm hai đồng chẵn", còn thiếu chữ "lẻ". mọi người giúp mình với.

[FONT=&quot] '*******************************[/FONT]
[FONT=&quot] ' Main Function for vndnumber *[/FONT]
[FONT=&quot] '*******************************[/FONT]

[FONT=&quot] Public Function vndnumber(ByVal MyNumber)[/FONT]
[FONT=&quot] Dim Dong, Temp[/FONT]
[FONT=&quot] Dim DecimalPlace, Count[/FONT]

[FONT=&quot] ReDim Place(9) As String[/FONT]
[FONT=&quot] Place(2) = " Ngan "[/FONT]
[FONT=&quot] Place(3) = " Trieu "[/FONT]
[FONT=&quot] Place(4) = " Ty "[/FONT]
[FONT=&quot] Place(5) = " Ngan Ty "[/FONT]

[FONT=&quot] ' String representation of amount.[/FONT]
[FONT=&quot] MyNumber = Trim(Str(MyNumber))[/FONT]

[FONT=&quot] Count = 1[/FONT]
[FONT=&quot] Do While MyNumber <> ""[/FONT]
[FONT=&quot] Temp = vndGetHundreds(Right(MyNumber, 3))[/FONT]
[FONT=&quot] If Temp <> "" Then Dong = Temp & Place(Count) & Dong[/FONT]
[FONT=&quot] If Len(MyNumber) > 3 Then[/FONT]
[FONT=&quot] MyNumber = Left(MyNumber, Len(MyNumber) - 3)[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] MyNumber = ""[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] Count = Count + 1[/FONT]
[FONT=&quot] Loop[/FONT]

[FONT=&quot] Select Case Dong[/FONT]
[FONT=&quot] Case ""[/FONT]
[FONT=&quot] Dong = "Khong Dong "[/FONT]
[FONT=&quot] Case "One"[/FONT]
[FONT=&quot] Dong = "Mot Dong Chan"[/FONT]
[FONT=&quot] Case Else[/FONT]
[FONT=&quot] Dong = Dong & " Dong Chan"[/FONT]
[FONT=&quot] End Select[/FONT]

[FONT=&quot] vndnumber = Dong[/FONT]
[FONT=&quot] End Function[/FONT]



[FONT=&quot] '*******************************************[/FONT]
[FONT=&quot] ' Converts a number from 100-999 into text *[/FONT]
[FONT=&quot] '*******************************************[/FONT]

[FONT=&quot] Public Function vndGetHundreds(ByVal MyNumber)[/FONT]
[FONT=&quot] Dim Result As String[/FONT]

[FONT=&quot] If Val(MyNumber) = 0 Then Exit Function[/FONT]
[FONT=&quot] MyNumber = Right("000" & MyNumber, 3)[/FONT]

[FONT=&quot] ' Convert the hundreds place.[/FONT]
[FONT=&quot] If Mid(MyNumber, 1, 1) <> "0" Then[/FONT]
[FONT=&quot] Result = vndGetDigit(Mid(MyNumber, 1, 1)) & " Tram "[/FONT]
[FONT=&quot] End If[/FONT]

[FONT=&quot] ' Convert the tens and ones place.[/FONT]
[FONT=&quot] If Mid(MyNumber, 2, 1) <> "0" Then[/FONT]
[FONT=&quot] Result = Result & vndGetTens(Mid(MyNumber, 2))[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] Result = Result & vndGetDigit(Mid(MyNumber, 3))[/FONT]
[FONT=&quot] End If[/FONT]

[FONT=&quot] vndGetHundreds = Result[/FONT]
[FONT=&quot] End Function[/FONT]



[FONT=&quot] '*********************************************[/FONT]
[FONT=&quot] ' Converts a number from 10 to 99 into text. *[/FONT]
[FONT=&quot] '*********************************************[/FONT]

[FONT=&quot] Public Function vndGetTens(TensText)[/FONT]
[FONT=&quot] Dim Result As String[/FONT]

[FONT=&quot] Result = "" ' Null out the temporary function value.[/FONT]
[FONT=&quot] If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...[/FONT]
[FONT=&quot] Select Case Val(TensText)[/FONT]
[FONT=&quot] Case 10: Result = "Muoi"[/FONT]
[FONT=&quot] Case 11: Result = "Muoi Mot"[/FONT]
[FONT=&quot] Case 12: Result = "Muoi Hai"[/FONT]
[FONT=&quot] Case 13: Result = "Muoi Ba"[/FONT]
[FONT=&quot] Case 14: Result = "Muoi Bon"[/FONT]
[FONT=&quot] Case 15: Result = "Muoi Lam"[/FONT]
[FONT=&quot] Case 16: Result = "Muoi Sau"[/FONT]
[FONT=&quot] Case 17: Result = "Muoi Bay"[/FONT]
[FONT=&quot] Case 18: Result = "Muoi Tam"[/FONT]
[FONT=&quot] Case 19: Result = "Muoi Chin"[/FONT]
[FONT=&quot] Case Else[/FONT]
[FONT=&quot] End Select[/FONT]
[FONT=&quot] Else ' If value between 20-99...[/FONT]
[FONT=&quot] Select Case Val(Left(TensText, 1))[/FONT]
[FONT=&quot] Case 2: Result = "Hai Muoi "[/FONT]
[FONT=&quot] Case 3: Result = "Ba Muoi "[/FONT]
[FONT=&quot] Case 4: Result = "Bon Muoi "[/FONT]
[FONT=&quot] Case 5: Result = "Nam Muoi "[/FONT]
[FONT=&quot] Case 6: Result = "Sau Muoi "[/FONT]
[FONT=&quot] Case 7: Result = "Bay Muoi "[/FONT]
[FONT=&quot] Case 8: Result = "Tam Muoi "[/FONT]
[FONT=&quot] Case 9: Result = "Chin Muoi "[/FONT]
[FONT=&quot] Case Else[/FONT]
[FONT=&quot] End Select[/FONT]
[FONT=&quot] Result = Result & vndGetDigit _[/FONT]
[FONT=&quot] (Right(TensText, 1)) ' Retrieve ones place.[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] vndGetTens = Result[/FONT]
[FONT=&quot] End Function[/FONT]




[FONT=&quot] '*******************************************[/FONT]
[FONT=&quot] ' Converts a number from 1 to 9 into text. *[/FONT]
[FONT=&quot] '*******************************************[/FONT]

[FONT=&quot] Public Function vndGetDigit(Digit)[/FONT]
[FONT=&quot] Select Case Val(Digit)[/FONT]
[FONT=&quot] Case 1: vndGetDigit = "Mot"[/FONT]
[FONT=&quot] Case 2: vndGetDigit = "Hai"[/FONT]
[FONT=&quot] Case 3: vndGetDigit = "Ba"[/FONT]
[FONT=&quot] Case 4: vndGetDigit = "Bon"[/FONT]
[FONT=&quot] Case 5: vndGetDigit = "Nam"[/FONT]
[FONT=&quot] Case 6: vndGetDigit = "Sau"[/FONT]
[FONT=&quot] Case 7: vndGetDigit = "Bay"[/FONT]
[FONT=&quot] Case 8: vndGetDigit = "Tam"[/FONT]
[FONT=&quot] Case 9: vndGetDigit = "Chin"[/FONT]
[FONT=&quot] Case Else: vndGetDigit = ""[/FONT]
[FONT=&quot] End Select[/FONT]
[FONT=&quot] End Function[/FONT]
 
Lần chỉnh sửa cuối:
Thêm phần màu đỏ vào đoạn code này:
Mã:
Public Function vndGetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
[COLOR=green]' Convert the hundreds place.[/COLOR]
If Mid(MyNumber, 1, 1) <> "0" Then
Result = vndGetDigit(Mid(MyNumber, 1, 1)) & " Tram "
End If
[COLOR=green]' Convert the tens and ones place.[/COLOR]
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & vndGetTens(Mid(MyNumber, 2))
[COLOR=red]ElseIf (Mid(MyNumber, 2, 1) = "0" And Mid(MyNumber, 1, 1) <> "0") _[/COLOR]
[COLOR=red]And Mid(MyNumber, 3, 1) <> "0" _[/COLOR]
[COLOR=red]Then Result = Result & "Le " & vndGetDigit(Mid(MyNumber, 3))[/COLOR]
Else
Result = Result & vndGetDigit(Mid(MyNumber, 3))
End If
vndGetHundreds = Result
End Function
 
Lần chỉnh sửa cuối:
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom