pls any one can help making a simple converter from text to numeric converter,
like if i input, one hundred three it will display it in numeric...
thank you in advance ^^

Recommended Answers

All 9 Replies

Uh...we don't really do that sort of thing here. We're more about "If you're stuck, show us what you've done and we'll help you get un-stuck". If you're looking for sample code, you might try a google search first.

Good luck to you anyway!

commented: I like the way you phrased this! +17

What is the largest number you need to convert?

Will you also need to convert fractional numbers?

Or, Decimal Numbers like Ten Point Four?

What are the variant in input text?

Will you accept the above entry as well as Ten dot 4?

Or, One Hundred and 4 vs One Hundred 4?


I did this once back when I was writing childrens programs in QuickBasic but I don't have that code here at work.

It' really not that hard once you properly define the problem. :)

You mean "One hundred three" >> "103" ??
You can start by splitting the text string into segments, and work with them one by one using If.. Then.. statements.

I'll work on some code tonight, if, time allows.

This seems simple, at first, but as you consider all the ways numbers can be written out you quickly realize that the basic Select case statement isn't going to do the job.

That is why some agreement has to be made as to range and whether 41 has to be written as forty-one or fortyone or forty one... The first is the "accepted" method per http://www.merriam-webster.com/mw/table/number.htm

But, your users could make up their own rules...

Basically right now I am looking at around four Select Case sets...

1 through 19 with prefixes for 20 to 90 (the 20-90 would allbe one statement that would call to the next case section - a total of twenty cases)

another one to differentiate between the 20 to 90 prefixes (eight cases)

then 1 through 9 to add to the prefix value (nine casses)

and lastly the modifier... depending upon range hundred, thousand, million would be three cases...

Ie, if you have one (value equals 1) followed by hundred then you would take the 1 and multiply by 100...

hmm that's four large case statements to start with.

smm

OK, here is some working code to convert a String Number into a Value.

One Hundred Three Thousand --> 103000

It is written for non-fractional numbers...

You'll have to change all your numbers to Double and add a whole extra section for after the Point/Dot to handle fractions.

I expect that you will have some debugging to do. :) (It'll be good for you...)

I didn't really have time to extensively debug it myself.

You may also want to add some error checking in the Case Else's or some traps for mis-spelled words, etc.

Also, it expects compond numbers, like 53, to be hyphenated...

As this is my first pass after many many years, I expect that there are some areas that can be cleaned up...

Feel free... :)

AS you read through the code you'll realize that it is on a (test) form with only a textbox and a label.

There is also no code to handle when you do a number line "One Hundred Five Million Two Hundred Fifty Thousand". You'll have to add code to "see" when your Multiplier is les than it was previously. pMultiplier was supposed to do that. But, ran out of time.

Option Explicit

Public fValue As Long
Public pMultiplier As Long


Private Sub Text1_KeyPress(KeyAscii As Integer)
    fValue = 0
    If KeyAscii = 13 Then Label1.Caption = StringToNum(Text1.Text)
End Sub

Function StringToNum(uIn As String) As Long
    ' Do !NOT! make uIn Public or Global!
    Dim StartAt As Long
    Dim StoppedAt As Long
    Dim RetStr As String
    Dim xStr As String
    Dim uInp As String
    
    StartAt = 1
    RetStr = " "
    pMultiplier = 0
    
    Do
        ' These are all passed by reference as the default
        xStr = Scan_To(uIn, StartAt, StoppedAt, RetStr)
        uInp = ""
        If RetStr <> " " Then
            StartAt = StoppedAt + 1
            RetStr = " "
            uInp = Scan_To(uIn, StartAt, StoppedAt, RetStr)
        End If
        If Multiplier(xStr) = 0 Then
            fValue = fValue + LeadNum(xStr, uInp)
        Else
            fValue = fValue * Multiplier(xStr)
        End If
        StartAt = StoppedAt + 1
    Loop Until StartAt > Len(uIn)
    
    StringToNum = fValue
End Function

Function LeadNum(uIn As String, Optional uInp As String = "") As Long
    LeadNum = 0
    Select Case UCase(uIn)
        Case "ONE"
            LeadNum = 1
        Case "TWO"
            LeadNum = 2
        Case "THREE"
            LeadNum = 3
        Case "FOUR"
            LeadNum = 4
        Case "FIVE"
            LeadNum = 5
        Case "SIX"
            LeadNum = 6
        Case "SEVEN"
            LeadNum = 7
        Case "EIGHT"
            LeadNum = 8
        Case "NINE"
            LeadNum = 9
        Case "TEN"
            LeadNum = 10
        Case "ELEVEN"
            LeadNum = 11
        Case "TWELEVE"
            LeadNum = 12
        Case "THIRTEEN"
            LeadNum = 13
        Case "FOURTEEN"
            LeadNum = 14
        Case "FIFTEEN"
            LeadNum = 15
        Case "SIXTEEN"
            LeadNum = 16
        Case "SEVENTEEN"
            LeadNum = 17
        Case "EIGHTEEN"
            LeadNum = 18
        Case "NINETEEN"
            LeadNum = 19
        Case "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"
            LeadNum = CompoundNum(uIn, uInp)
        Case Else
            'need to check for Modifier Word
    End Select
End Function

Function CompoundNum(uIn, uInp As String) As Long
    CompoundNum = 0
    Select Case UCase(uIn)
        Case "TWENTY"
            CompoundNum = 20
        Case "THIRTY"
            CompoundNum = 30
        Case "FORTY"
            CompoundNum = 40
        Case "FIFTY"
            CompoundNum = 50
        Case "SIXTY"
            CompoundNum = 60
        Case "SEVENTY"
            CompoundNum = 70
        Case "EIGHTY"
            CompoundNum = 80
        Case "NINETY"
            CompoundNum = 90
    End Select
    CompoundNum = CompoundNum + CompoundNumAdd(uInp)
End Function
Function CompoundNumAdd(uInp) As Long
    CompoundNumAdd = 0
    Select Case UCase(uInp)
        Case "ONE"
            CompoundNumAdd = 1
        Case "TWO"
            CompoundNumAdd = 2
        Case "THREE"
            CompoundNumAdd = 3
        Case "FOUR"
            CompoundNumAdd = 4
        Case "FIVE"
            CompoundNumAdd = 5
        Case "SIX"
            CompoundNumAdd = 6
        Case "SEVEN"
            CompoundNumAdd = 7
        Case "EIGHT"
            CompoundNumAdd = 8
        Case "NINE"
            CompoundNumAdd = 9
    End Select
End Function

Function Multiplier(uIn As String) As Long
    Dim x As Long
    
    Multiplier = 0
    Select Case UCase(uIn)
        Case "HUNDRED"
            Multiplier = 100
        Case "THOUSAND"
            Multiplier = 1000#
        Case "MILLION"
            Multiplier = 1000000#
        Case "BILLION"
            Multiplier = 1000000000#
        Case "TRILLION"
            Multiplier = 1000000000000#
        Case Else
            Debug.Print uIn & " has no match"
    End Select
    x = Multiplier
    Debug.Print "Multiplier: " & x
End Function

Function Scan_To(uIn As String, StartAt As Long, StoppedAt As Long, RetStr As String) As String
    'This Function Scans a Text String for a Specific sub-String
    Dim xStr As String
    Dim n As Long
    
    For n = StartAt To (Len(uIn) - Len(RetStr)) + 1
        If Mid(uIn, n, Len(RetStr)) = RetStr Then Scan_To = xStr: StoppedAt = n: Exit Function
        ' Added functionality added for compound numbers
        If Mid(uIn, n, Len(RetStr)) = "-" Then
            RetStr = "-"
            Scan_To = xStr: StoppedAt = n: Exit Function
        End If
        xStr = xStr & Mid(uIn, n, 1)
    Next n
    StoppedAt = n - 1
    Scan_To = xStr
End Function

I have to wonder whether it is worth posting "possible" solutions or not as I have not gotten any feed back on my posts???

This is what happens when you supply the code to someone without him showing any effort at all. Forget of feedback, i seriously doubt if that guy remembers this thread and is ever going to return back read to your answer.

I have to wonder whether it is worth posting "possible" solutions or not as I have not gotten any feed back on my posts???

Aw sori sir^^ im currently working the codes that you make for me ^^...
i cant post reply coz i cant log in using my old account here...

im working on it in how to display the output, all i do is i make textBox, a label, and a command where i put the codes that u have posted...

thank you sir SpiritualMadMan for answering my thread.. i always visited here but i cant post reply, account problem aheheh...

This is what happens when you supply the code to someone without him showing any effort at all. Forget of feedback, i seriously doubt if that guy remembers this thread and is ever going to return back read to your answer.

i always visit here... coz i found this thread helpfull to me who is still willing to learn about BV, but sad to say codes are to delacate to handle ahehe error occur always aheheh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.