Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert digit to text e.g. 7 to seven

Status
Not open for further replies.

mailzero28

Programmer
Oct 22, 2001
47
0
0
GB
Hi there,

I need to convert digits to text - anybody know how.
Its for cheque printing purposes, so 110 would have to read One Hundred and Ten.

Any help would be appreciated.
 
You'll have to use a bunch of Case and/or If statements.

Cast the number to string and see the length
Len(Cstr(110))
If the length is 3 (like above) then you know the first return from your loop with case statements is hundreds, the second tens and the third ones.

Your loop of case statements will look something like this(psuedo code)
for each digit in 110
Case is 1 return "one"
Case is 2 return "two"
...
Case is 0 return "zero"
end loop

So you'll get a &quot;one&quot;. Knowing it's hundred you start your String with &quot;one hundred&quot;. Your second return is <> &quot;zero&quot; add &quot; and &quot;. (String = &quot;one hundred and &quot;
Knowing the second return is tens go through a case statement again.
Case is &quot;one&quot; return &quot;ten&quot;
Case is &quot;two&quot; return &quot;twenty&quot;...

Think you should get the picture now
 
hi
I did before exactly what you request but it is not completed
a function to convert a salary digits to string
it will works with 5 digits number ( some bugs are there check it out

Public Function ConvertNumber(NumToConvert As String) As String
Dim S(1 To 5) As String
Dim digit As Long
Dim final As String
If lang = 0 Then
g1 = 1
For q = Len(NumToConvert) To 1 Step -1
g = Mid(NumToConvert, q, 1)
Select Case g1
Case 2
If g <> 0 Then
If g = 1 Then
S(g1) = &quot;ten&quot;
ElseIf g = 2 Then
S(g1) = &quot;twenty&quot;
ElseIf g = 3 Then
S(g1) = &quot;thirty&quot;
ElseIf g = 4 Then
S(g1) = &quot;fourty&quot;
ElseIf g = 5 Then
S(g1) = &quot;fifty&quot;
ElseIf g = 6 Then
S(g1) = &quot;sixty&quot;
ElseIf g = 7 Then
S(g1) = &quot;seventy&quot;
ElseIf g = 8 Then
S(g1) = &quot;eighty&quot;
ElseIf g = 9 Then
S(g1) = &quot;ninty&quot;
End If
Else
S(g1) = &quot;&quot;
End If
Case 3
If g <> 0 Then
If g = 1 Then
S(g1) = &quot;one hundred&quot;
ElseIf g = 2 Then
S(g1) = &quot;tow hundreds&quot;
ElseIf g = 3 Then
S(g1) = &quot;three hundreds&quot;
ElseIf g = 4 Then
S(g1) = &quot;four hundreds&quot;
ElseIf g = 5 Then
S(g1) = &quot;five hundreds&quot;
ElseIf g = 6 Then
S(g1) = &quot;six hundreds&quot;
ElseIf g = 7 Then
S(g1) = &quot;seven hundreds&quot;
ElseIf g = 8 Then
S(g1) = &quot;eight hundreds&quot;
ElseIf g = 9 Then
S(g1) = &quot;nine hundreds&quot;
End If
Else
S(g1) = &quot;&quot;
End If
Case 4
If NumToConvert > 999 And NumToConvert < 10000 Then
If g <> 0 Then
If g = 1 Then
S(g1) = &quot;one thousand&quot;
ElseIf g = 2 Then
S(g1) = &quot;two thousands&quot;
ElseIf g = 3 Then
S(g1) = &quot;three thousands&quot;
ElseIf g = 4 Then
S(g1) = &quot;four thousands&quot;
ElseIf g = 5 Then
S(g1) = &quot;five thousands&quot;
ElseIf g = 6 Then
S(g1) = &quot;six thousands&quot;
ElseIf g = 7 Then
S(g1) = &quot;seven thousands&quot;
ElseIf g = 8 Then
S(g1) = &quot;eight thousands&quot;
ElseIf g = 9 Then
S(g1) = &quot;nine thousands&quot;
End If
Else
g(g1) = &quot;&quot;
End If
ElseIf NumToConvert > 9999 And NumToConvert < 100000 Then

If g <> 0 Then
If g = 1 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;eleven&quot;
Else
S(g1) = &quot;one&quot;
End If
ElseIf g = 2 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;twelve&quot;
Else
S(g1) = &quot;two&quot;
End If
ElseIf g = 3 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;thirteen&quot;
Else
S(g1) = &quot;three&quot;
End If
ElseIf g = 4 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;fourteen&quot;
Else
S(g1) = &quot;four&quot;
End If
ElseIf g = 5 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;fifteen&quot;
Else
S(g1) = &quot;five&quot;
End If
ElseIf g = 6 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;sixteen&quot;
Else
S(g1) = &quot;six&quot;
End If
ElseIf g = 7 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;seventeen&quot;
Else
S(g1) = &quot;seven&quot;
End If
ElseIf g = 8 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;eighteen&quot;
Else
S(g1) = &quot;eight&quot;
End If
ElseIf g = 9 Then
If Mid(NumToConvert, 1, 1) = 1 Then
S(g1) = &quot;ninteen&quot;
Else
S(g1) = &quot;nine&quot;
End If
End If
Else
S(g1) = &quot;&quot;
End If
End If
Case 5
If g <> 0 Then
If g = 1 Then
S(g1) = &quot;ten&quot;
ElseIf g = 2 Then
S(g1) = &quot;twenty&quot;
ElseIf g = 3 Then
S(g1) = &quot;thirty&quot;
ElseIf g = 4 Then
S(g1) = &quot;fourty&quot;
ElseIf g = 5 Then
S(g1) = &quot;fifty&quot;
ElseIf g = 6 Then
S(g1) = &quot;sixty&quot;
ElseIf g = 7 Then
S(g1) = &quot;seventy&quot;
ElseIf g = 8 Then
S(g1) = &quot;eighty&quot;
ElseIf g = 9 Then
S(g1) = &quot;ninty&quot;
End If
Else
S(g1) = &quot;&quot;
End If

End Select
g1 = g1 + 1

Next
If Val(NumToConvert) > 9999 Then
If Mid(NumToConvert, 1, 1) = 1 And Mid(NumToConvert, 2, 1) <> &quot;0&quot; Then
S(Len(NumToConvert)) = &quot;&quot;
End If
End If
If Val(NumToConvert) > 9999 Then
S(4) = S(5) & &quot; &quot; & ANDRELATION & S(4) & &quot; thousands&quot;
End If



For Y = 4 To 2 Step -1
If final <> &quot;&quot; Then
If S(Y) <> &quot;&quot; And anddone = 1 Then
final = &quot; &quot; & final & &quot; and &quot; & S(Y)
anddone = 1
ElseIf S(Y) <> &quot;&quot; Then
final = &quot; &quot; & final & &quot; &quot; & S(Y)
anddone = 1
End If
Else
final = S(Y)
End If
Next
ConvertNumber = final
End If
End Function

hope I did help
 
see faq181-1740




MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top