Solved

# ! ! ! Converting Nos. to Words? ! ! !

Posted on 2000-02-16
157 Views
Expert Friends,

I would like to know that wether any built in functions in VB is
available to convert any given no. into word.  For example
if we give a no. say 55, I want to convert it into "Fifty Five";
174 - One Hundred and Seventy Four.

If it is not available can any body give the VB Source
code for the above said?

Srinivasan
0
Question by:eesrinivaassan

LVL 2

Expert Comment

ID: 2529805
i think there is no in built function for this. what's the maximum value of the number to be converted?

alok.
0

LVL 5

Expert Comment

ID: 2529893
Something like this might help. Far from complete, but it should get you started:

Select Case Mid\$(Text1.Text, Len(Text1.Text), 1)
Case 0
NewNum = ""
Case 1
NewNum = " one"
Case 2
NewNum = " two"
End Select
Select Case Mid\$(Text1.Text, Len(Text1.Text) - 1, 1)
Case 0
NewNum = " " & NewNum
Case 1
'Special code needed for "teens"
Case 2
NewNum = " twenty" & NewNum
Case 2
NewNum = " thirty" & NewNum
End Select
MsgBox NewNum

HTH!
0

LVL 27

Expert Comment

ID: 2529920
0

LVL 9

Expert Comment

ID: 2529940
0

LVL 14

Accepted Solution

mcrider earned 50 total points
ID: 2529951
Here you go... This function convert values to word values four different ways:

* "Paragraph style numbers" - Zero through nine. 10 and greater are the actual numbers as a string.

FORMAT: X\$=NumberToWord(500)

* "Verbose Numbers" - All numbers are converted to words, and will convert up to nine hundred and ninety-nine trillion.

FORMAT: X\$=NumberToWord(500,TRUE)

* "Say as dollars" - Convert to a word dollar value. For example: if the value is 500, "five hundred dollars" is returned.

FORMAT: X\$=NumberToWord(500,TRUE,TRUE)

* "Say Zero Cents" - Convert to a word dollar value. For example: if the value is 500, "five hundred dollars and zero cents" is returned.

FORMAT: X\$=NumberToWord(500,TRUE,TRUE,TRUE)

Cheers!

THE CODE:

Function NumberToWord(Value As Variant, Optional Verbose As Boolean, _
Optional IsCurrency As Boolean, Optional SayCentsIfZero As Boolean) As String
Dim InternalValue As Variant
Dim Ival As Variant
Dim Jval As Variant
Dim NTWbuf As String
Dim Decimals As Variant
Dim DecString As String

InternalValue = Fix(CDec(Value))
Decimals = Value - InternalValue

If IsCurrency = True Then
DecString = Trim\$(Str\$(Decimals))
Ival = Val(Left\$(Mid\$(DecString, 2) + "00", 2))
NTWbuf = NumberToWord(InternalValue, True) + " dollars"
If Ival = 0 Then
If SayCentsIfZero = True Then
NTWbuf = NTWbuf + " and no cents"
End If
Else
NTWbuf = NTWbuf + " and "
NTWbuf = NTWbuf + NumberToWord(Ival, True) + " cents"
End If
NumberToWord = NTWbuf
Exit Function
End If

If Decimals > 0 Then
DecString = Trim\$(Str\$(Decimals))
DecString = Mid\$(DecString, 2)
NTWbuf = NumberToWord(InternalValue, True) + " point"
For Ival = 1 To Len(DecString)
NTWbuf = NTWbuf + " " + NumberToWord(Val(Mid\$(DecString, Ival, 1)), True)
Next Ival
NTWbuf = Trim\$(NTWbuf)
NumberToWord = NTWbuf
Exit Function
End If

If Verbose = True Then
Select Case InternalValue
Case 0
NumberToWord = "zero"
Case 1
NumberToWord = "one"
Case 2
NumberToWord = "two"
Case 3
NumberToWord = "three"
Case 4
NumberToWord = "four"
Case 5
NumberToWord = "five"
Case 6
NumberToWord = "six"
Case 7
NumberToWord = "seven"
Case 8
NumberToWord = "eight"
Case 9
NumberToWord = "nine"
Case 10
NumberToWord = "ten"
Case 11
NumberToWord = "eleven"
Case 12
NumberToWord = "twelve"
Case 13
NumberToWord = "thirteen"
Case 14
NumberToWord = "fourteen"
Case 15
NumberToWord = "fifteen"
Case 16
NumberToWord = "sixteen"
Case 17
NumberToWord = "seventeen"
Case 18
NumberToWord = "eighteen"
Case 19
NumberToWord = "ninteen"
Case 20 To 29
NTWbuf = "twenty-"
Ival = InternalValue
Ival = Ival - 20
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 30 To 39
NTWbuf = "thirty-"
Ival = InternalValue
Ival = Ival - 30
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 40 To 49
NTWbuf = "fourty-"
Ival = InternalValue
Ival = Ival - 40
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 50 To 59
NTWbuf = "fifty-"
Ival = InternalValue
Ival = Ival - 50
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 60 To 69
NTWbuf = "sixty-"
Ival = InternalValue
Ival = Ival - 60
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 70 To 79
NTWbuf = "seventy-"
Ival = InternalValue
Ival = Ival - 70
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 80 To 89
NTWbuf = "eighty-"
Ival = InternalValue
Ival = Ival - 80
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 90 To 99
NTWbuf = "ninety-"
Ival = InternalValue
Ival = Ival - 90
NTWbuf = NTWbuf + NumberToWord(Ival, True)
If Right\$(NTWbuf, 1) = "-" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 1)
If Right\$(NTWbuf, 5) = "-zero" Then NTWbuf = Left\$(NTWbuf, Len(NTWbuf) - 5)
NumberToWord = NTWbuf
Case 100 To 999
Ival = Fix(InternalValue / 100)
Jval = InternalValue - (Ival * 100)
NTWbuf = NumberToWord(Ival, True) + " hundred "
If Jval <> 0 Then
If Jval < 100 Then NTWbuf = NTWbuf + "and "
NTWbuf = NTWbuf + NumberToWord(Jval, True)
End If
NumberToWord = Trim\$(NTWbuf)
Case 1000 To 999999
Ival = Fix(InternalValue / 1000)
Jval = InternalValue - (Ival * 1000)
NTWbuf = NumberToWord(Ival, True) + " thousand "
If Jval <> 0 Then
If Jval < 100 Then NTWbuf = NTWbuf + "and "
NTWbuf = NTWbuf + NumberToWord(Jval, True)
End If
NumberToWord = Trim\$(NTWbuf)
Case 1000000 To 999999999
Ival = Fix(InternalValue / 1000000)
Jval = InternalValue - (Ival * 1000000)
NTWbuf = NumberToWord(Ival, True) + " million "
If Jval <> 0 Then
If Jval < 100 Then NTWbuf = NTWbuf + "and "
NTWbuf = NTWbuf + NumberToWord(Jval, True)
End If
NumberToWord = Trim\$(NTWbuf)
'Case 1000000000 To 100,000,000,000
Case 1000000000 To 999999999999#
Ival = Fix(InternalValue / 1000000000)
Jval = InternalValue - (Ival * 1000000000)
NTWbuf = NumberToWord(Ival, True) + " billion "
If Jval <> 0 Then
If Jval < 100 Then NTWbuf = NTWbuf + "and "
NTWbuf = NTWbuf + NumberToWord(Jval, True)
End If
NumberToWord = Trim\$(NTWbuf)
'Case 1000000000000# To 100,000,000,000,000#
Case 1000000000000# To 999999999999999#
Ival = Fix(InternalValue / 1000000000000#)
Jval = InternalValue - (Ival * 1000000000000#)
NTWbuf = NumberToWord(Ival, True) + " trillion "
If Jval <> 0 Then
If Jval < 100 Then NTWbuf = NTWbuf + "and "
NTWbuf = NTWbuf + NumberToWord(Jval, True)
End If
NumberToWord = Trim\$(NTWbuf)
Case Else
NumberToWord = Trim\$(Str\$(InternalValue))
End Select
Else
Select Case InternalValue
Case 0
NumberToWord = "zero"
Case 1
NumberToWord = "one"
Case 2
NumberToWord = "two"
Case 3
NumberToWord = "three"
Case 4
NumberToWord = "four"
Case 5
NumberToWord = "five"
Case 6
NumberToWord = "six"
Case 7
NumberToWord = "seven"
Case 8
NumberToWord = "eight"
Case 9
NumberToWord = "nine"
Case Else
NumberToWord = Trim\$(Str\$(InternalValue))
End Select
End If
End Function
0

LVL 9

Expert Comment

ID: 2530061
Mcrider: It is not necessary to mark your solution as answer, you should mark it as comments. If eesrinivaassan will find your solution the best solution for this situation, she/he will ask you to mark it as answer or she/he can accept your solution as answer.

Eesrinivaassan:
0

LVL 14

Expert Comment

ID: 2530105
Ruchi,

I know when to post and not to post answers.  I suggest you look at the top 15 experts in this category.  URLs come and go.  What I have posted is a COMPLETE solution for the question.

I really hate it when I buy an answer to a question and the answer is a URL that is no longer available.  Code is ALWAYS better solution!  You may say, "but i've seen you post URLs as well..."

I only post URLs to Microsoft KB articles... If Microsoft "goes away", so does VB.

Cheers!
0

LVL 9

Expert Comment

ID: 2530137
Mcrider: Fair enough. I looked at the top 15 experts in the VB topic area -- Wow! You are #3! Congratulations, Mcrider! Maybe I should start to post URLS to Ms KB articles. :-)
0

LVL 18

Expert Comment

ID: 2530603
If Microsoft goes away - then the whole world goes away!

I'd *always* lock if the solution is complete code.
0

LVL 14

Expert Comment

ID: 2530664
Yea, I'd DEFINATELY call Microsoft going away an ELE ;-)

Cheers!
0

## Featured Post

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…