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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Number to Strings(Ex - 9 to Nine)

Code for Converting Number to

Converting Number to Strings(Ex - 9 to Nine)

by  San22  Posted    (Edited  )
Hello Everybody,
Firstly I thank all of whom have helped in finding a code for converting numbers to string.
I here am giving a totally different code which works perfectly till 9 Crores

The Code is ---
Option Compare Database
Public Function Spell(InVal) As String

Dim Ones(100) As Variant
Dim X(10) As Variant
Dim Real, Imax, Idigit(30), J, I1
Dim Value As String

X(1) = ""
X(2) = ""
X(3) = " Hundred "
X(4) = " Thousand "
X(5) = ""
X(6) = " Lakh "
X(7) = ""
X(8) = " Crore "
X(9) = ""

Ones(1) = " One "
Ones(2) = " Two "
Ones(3) = " Three "
Ones(4) = " Four "
Ones(5) = " Five "
Ones(6) = " Six "
Ones(7) = " Seven "
Ones(8) = " Eight "
Ones(9) = " Nine "
Ones(10) = " Ten "
Ones(11) = " Eleven "
Ones(12) = " Twelve "
Ones(13) = " Thirteen "
Ones(14) = " Fourteen "
Ones(15) = " Fifteen "
Ones(16) = " Sixteen "
Ones(17) = " Seventeen "
Ones(18) = " Eighteen "
Ones(19) = " Nineteen "
Ones(20) = " Twenty "
Ones(30) = " Thirty "
Ones(40) = " Fourty "
Ones(50) = " Fifty "
Ones(60) = " Sixty "
Ones(70) = " Seventy "
Ones(80) = " Eighty "
Ones(90) = " Ninety "
Ones(100) = " Hundred "

Real = Int(InVal)
Imax = Len(Real)

Value = ""
For i = 1 To Imax
Idigit(i) = Real - 10 * Int(Real / 10)
Real = Int(Real / 10)
Next i

For i = 1 To Imax
I1 = Imax - i + 1
J = Idigit(I1)
If I1 <> 9 Then GoTo 60
If J <> 1 Then GoTo 65
J = 10 + Idigit(I1 - 1)
i = i + 1
I1 = I1 - 1
GoTo 99
65:
J = 10 * J
If Idigit(I1) = 0 And Idigit(I1 - 1) = 0 Then
i = i + 1
GoTo 100
End If
60:
If I1 <> 7 Then GoTo 80
If J <> 1 Then GoTo 85
J = 10 + Idigit(I1 - 1)
i = i + 1
I1 = I1 - 1
GoTo 99
85:
J = 10 * J
If Idigit(I1) = 0 And Idigit(I1 - 1) = 0 Then
i = i + 1
GoTo 100
End If
80:
If I1 <> 5 Then GoTo 90
If J <> 1 Then GoTo 75
J = 10 + Idigit(I1 - 1)
i = i + 1
I1 = I1 - 1
GoTo 99
75:
J = 10 * J
If Idigit(I1) = 0 And Idigit(I1 - 1) = 0 Then
i = i + 1
GoTo 100
End If
90:
If I1 <> 3 Then GoTo 95
If Idigit(I1) = 0 Then GoTo 100
95:
If I1 <> 2 Then GoTo 99
If J <> 1 Then GoTo 98
J = 10 + Idigit(I1 - 1)
i = i + 1
I1 = I1 - 1
GoTo 99
98:
J = 10 * J
99:
Value = Value + Ones(J) + X(I1)
100:
Next i
Spell = Value
End Function
---
If any bugs r present plz let me know.
Thankz
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top