Solved

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

Posted on 2000-02-16
10
165 Views
Last Modified: 2010-05-02
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?

Please Help.

Srinivasan
0
Comment
Question by:eesrinivaassan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 2

Expert Comment

by:alokanant
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

by:KDivad
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 28

Expert Comment

by:Ark
ID: 2529920
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 9

Expert Comment

by:Ruchi
ID: 2529940
0
 
LVL 14

Accepted Solution

by:
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

by:Ruchi
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:
Ark and Divad got their comments first so look at them.
0
 
LVL 14

Expert Comment

by:mcrider
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

by:Ruchi
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

by:deighton
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

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


Cheers!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question