Functions - User Defined Functions
Author Message Functions - User Defined Functions

Greetings...

I could use some help with a question I have...

I'm trying to create a module which will take a number and convert it
into a word...
Such as: 1 = One,  45 = Fourty-Five.

So, I thought the best way to do this would be a function.

So, as a simple example I have this:

Public Function OneDigit(Number As Integer) As String
Select Case Number
Case 1
OneDigit = "One"
Case 2
OneDigit = "Two"
Case 3
OneDigit = "Three"
Case 4
OneDigit = "Four"
Case 5
OneDigit = "Five"
Case 6
OneDigit = "Six"
Case 7
OneDigit = "Seven"
Case 8
OneDigit = "Eight"
Case 9
OneDigit = "Nine"
End Select
End Function

Is this Valid?  How do I return some value from a function?
So, when I call this function I can do this:

Dim Number as Integer
Number = 3
Label1.Caption = OneDigit(Number)

Cajun Nights Mush            />       Who Wants to Live Forever?
Cajun.Targonia.com 7373     /<                ICQ:13682963

[\\\\\\\\\\(O):::<======================================-

BourbonStreet/1373/          \>               Blackthorne.

Wed, 18 Jun 1902 08:00:00 GMT  Functions - User Defined Functions
John,

Your function is OK, but I hope noboddy puts 6,445,301 in your inputbox or
whatever !

Hans

Quote:
> Greetings...

> I could use some help with a question I have...

> I'm trying to create a module which will take a number and convert it
> into a word...
> Such as: 1 = One,  45 = Fourty-Five.

> So, I thought the best way to do this would be a function.

> So, as a simple example I have this:

> Public Function OneDigit(Number As Integer) As String
>     Select Case Number
>         Case 1
>             OneDigit = "One"
>         Case 2
>             OneDigit = "Two"
>         Case 3
>             OneDigit = "Three"
>         Case 4
>             OneDigit = "Four"
>         Case 5
>             OneDigit = "Five"
>         Case 6
>             OneDigit = "Six"
>         Case 7
>             OneDigit = "Seven"
>         Case 8
>             OneDigit = "Eight"
>         Case 9
>             OneDigit = "Nine"
>     End Select
> End Function

> Is this Valid?  How do I return some value from a function?
> So, when I call this function I can do this:

> Dim Number as Integer
> Number = 3
> Label1.Caption = OneDigit(Number)

> Cajun Nights Mush            />       Who Wants to Live Forever?
> Cajun.Targonia.com 7373     /<                ICQ:13682963

> [\\\\\\\\\\(O):::<======================================-

> BourbonStreet/1373/          \>               Blackthorne.

Wed, 18 Jun 1902 08:00:00 GMT  Functions - User Defined Functions
If your interested, these functions make it work up to 9999.

Private Sub Form_Load()
Dim x As String
x = num_to_text(9999)
MsgBox x
End Sub

Function num_to_text(a_number As Integer) As String

Dim thous As Integer
Dim huns As Integer
Dim tens_and_units As Integer
Dim tens As Integer
Dim ones As Integer

num_as_txt = LTrim(Str(a_number))
len_of_num = Len(num_as_txt)

If len_of_num = 1 Then num_as_txt = "000" + num_as_txt
If len_of_num = 2 Then num_as_txt = "00" + num_as_txt
If len_of_num = 3 Then num_as_txt = "0" + num_as_txt
Debug.Print num_as_txt
thous = Val(Left(num_as_txt, 1))
huns = Val(Mid(num_as_txt, 2, 1))
tens = Val(Mid(num_as_txt, 3, 1))
ones = Val(Right(num_as_txt, 1))
Debug.Print thous, huns, tens, ones

and_ = ""
If thous > 0 Then
num_to_text = num_to_text + units(thous) + " thousand "
and_ = " and "
End If
If huns > 0 Then
num_to_text = num_to_text + units(huns) + " hundred "
and_ = " and "
End If
tens_and_units = tens * 10 + ones

If tens_and_units < 10 Then
num_to_text = num_to_text + and_ + units(tens_and_units)
Exit Function
End If

If tens_and_units < 20 Then
num_to_text = num_to_text + and_ + {*filter*}s(tens_and_units)
Exit Function
End If

num_to_text = num_to_text + and_ + tys(tens) + " " + units(ones)

End Function

Public Function tys(n As Integer) As String
Select Case n
Case 2
tys = "Twenty"
Case 3
tys = "Thirty"
Case 4
tys = "Fourty"
Case 5
tys = "Fifty"
Case 6
tys = "Sixty"
Case 7
tys = "Seventy"
Case 8
tys = "Eighty"
Case 9
tys = "Ninety"
End Select
End Function

Public Function units(n As Integer) As String
Select Case n
Case 0
units = ""
Case 1
units = "One"
Case 2
units = "Two"
Case 3
units = "Three"
Case 4
units = "Four"
Case 5
units = "Five"
Case 6
units = "Six"
Case 7
units = "Seven"
Case 8
units = "Eight"
Case 9
units = "Nine"
End Select
End Function

Public Function {*filter*}s(n As Integer) As String
Select Case n
Case 10
{*filter*}s = "Ten"
Case 11
{*filter*}s = "Eleven"
Case 12
{*filter*}s = "Twelve"
Case 13
{*filter*}s = "Thir{*filter*}"
Case 14
{*filter*}s = "Four{*filter*}"
Case 15
{*filter*}s = "Fif{*filter*}"
Case 16
{*filter*}s = "Six{*filter*}"
Case 17
{*filter*}s = "Seven{*filter*}"
Case 18
{*filter*}s = "Eigh{*filter*}"
Case 19
{*filter*}s = "Nine{*filter*}"
End Select
End Function

best regards

Steve.

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages

Powered by phpBB® Forum Software