VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Spells out a numeric value as english words.

by TWLambe (3 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 21st April 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Spells out a numeric value as english words.

Rate Spells out a numeric value as english words.






Private sBase1(4) As String          '(1)billion (2)million (3)thousand 4)/null/

Private sBase2(3) As String          '(1)hundred (2)ty      (3)/null/

Private sConvert(50, 2) As String    'eg., (n,1)"1ty 6"    (n,2)sixteen










Function SpellNumeric(lNumber) As String



'-------------------------------------------------------------------------------
'
'   FUNCTION :-     SpellNumeric
'   AUTHOR :-       TWLambe
'   DATE :-         21-Apr-2004 - (VB6 exercise for initialising private arrays)
'
'
'   DESCRIPTION :-
'
'   A function that accepts a LONG integer and returns an English text string
'   that spells out its value.
'
'
'   USAGE :-
'
'   returned spelt value string = SpellNumeric(nnnn)
'   Eg.,
'   "two hundred and seventy three million and forty one" = SpellNumeric(273000041)
'
'   NOTE :-
'   This function accepts a number upto 12 digits long.
'   This is purely an exercise and can probably be written more mathematically
'       with quicker processing, as this function is very "string" oriented.
'
'-------------------------------------------------------------------------------
            
            
Dim sNumb As String         'number as leading-zero string
Dim sResult As String       'working result string

Dim x, y As Integer         'counters, pointers, etc.

Dim bAndAdded As Boolean    '"and " added flag






                    '1st.time in - set up conversion arrays :-
    If sBase1(1) = Empty Then GoSub LOAD_ARRAYS
    









PROCESS:
                    'convert number as 12 characters with leading zeroes
    sNumb = Format$(lNumber, "000000000000")
                            
                            
    sResult = ""    'initialise result
                            
                    'work with each group of 3 numeric characters, ie.,
                    'billions, then millions, then thousands, then hundreds
    For x = 1 To 10 Step 3
    
        bAndAdded = False               'flag - has "and " been added to result yet ?
        
        For y = 0 To 2                  'for each character in block of 3 ...
            If Mid(sNumb, x + y, 1) <> "0" Then
                If y > 0 Then           'for tens and units, maybe prefix with "and "
                    Select Case x
                        Case 10         'add "and " if number > 100 ...
                                        'eg., two million AND thirty six
                                        'eg., one thousand AND one
                            If (Mid(sNumb, 11, 2) <> "00") _
                                And (Left(sNumb, 10) <> "0000000000") _
                                And (bAndAdded = False) Then
                                sResult = sResult + "and "
                                bAndAdded = True    '(prevent 2nd. "and " if eg. 136,
                                                    ' one hundred AND thirty AND six)
                            End If
                            
                        Case Else       'add "and " for millions etc, if > 100
                                        'eg., six hundred AND forty nine billion
                                        'eg., one hundred AND seven thousand
                            If (Mid(sNumb, x + 1, 2) <> "00") _
                                And (Mid(sNumb, x, 1) <> "0") _
                                And (bAndAdded = False) Then
                                sResult = sResult + "and "
                                bAndAdded = True    '(prevent 2nd. "and " if eg. 149,
                                                    ' one hundred AND forty AND nine)
                            End If
                    End Select
                End If
                
                sResult = sResult + Mid(sNumb, x + y, 1) + sBase2(y + 1)
                    'add "hundred", or "ty", or null - nb., "ty" will be converted
                    'at end to whichever tens unit, or -teen number it is
           End If
           
        Next y
        
                    'Add [billion][million][thousand] or [null] onto
                    'whichever 3-numeric-character section is currently
                    'being processed and is not "000"
        If Mid(sNumb, x, 3) <> "000" Then sResult = sResult + sBase1((x + 2) / 3)
    
    Next x          'next block of 3 numeric characters, ie., after billions
                    'do millions, then thousands, then hundreds
                           
                           
                    'The intermediate result string now contains
                    'rather jumbled words and numeric characters -
                    'convert these by a conversion array set up
                    'at 1st. call to this function :-
    For x = 1 To Val(sConvert(1, 0))
        sResult = Replace(sResult, sConvert(x, 1), sConvert(x, 2))
    Next x
    
    
FINALE:
                    'set function value and exit
    SpellNumeric = Trim(sResult)
    Exit Function
                            
                            
                            
'-------------------------------------------------------------------------------
                            
LOAD_ARRAYS:
        
        sBase1(1) = "billion "
        sBase1(2) = "million "
        sBase1(3) = "thousand "
        sBase1(4) = ""
    
        sBase2(1) = "hundred "
        sBase2(2) = "ty "
        sBase2(3) = ""
        
        sConvert(1, 1) = "1ty 1"
                            sConvert(1, 2) = "eleven "
        sConvert(2, 1) = "1ty 2"
                            sConvert(2, 2) = "twelve "
        sConvert(3, 1) = "1ty 3"
                            sConvert(3, 2) = "thirteen "
        sConvert(4, 1) = "1ty 4"
                            sConvert(4, 2) = "fourteen "
        sConvert(5, 1) = "1ty 5"
                            sConvert(5, 2) = "fifteen "
        sConvert(6, 1) = "1ty 6"
                            sConvert(6, 2) = "sixteen "
        sConvert(7, 1) = "1ty 7"
                            sConvert(7, 2) = "seventeen "
        sConvert(8, 1) = "1ty 8"
                            sConvert(8, 2) = "eighteen "
        sConvert(9, 1) = "1ty 9"
                            sConvert(9, 2) = "nineteen "
        sConvert(10, 1) = "2ty "
                            sConvert(10, 2) = "twenty "
        sConvert(11, 1) = "3ty "
                            sConvert(11, 2) = "thirty "
        sConvert(12, 1) = "4ty "
                            sConvert(12, 2) = "forty "
        sConvert(13, 1) = "5ty "
                            sConvert(13, 2) = "fifty "
        sConvert(14, 1) = "6ty "
                            sConvert(14, 2) = "sixty "
        sConvert(15, 1) = "7ty "
                            sConvert(15, 2) = "seventy "
        sConvert(16, 1) = "8ty "
                            sConvert(16, 2) = "eighty "
        sConvert(17, 1) = "9ty "
                            sConvert(17, 2) = "ninety "
        sConvert(18, 1) = "1ty "
                            sConvert(18, 2) = "ten "
        sConvert(19, 1) = "2"
                            sConvert(19, 2) = "two "
        sConvert(20, 1) = "3"
                            sConvert(20, 2) = "three "
        sConvert(21, 1) = "4"
                            sConvert(21, 2) = "four "
        sConvert(22, 1) = "5"
                            sConvert(22, 2) = "five "
        sConvert(23, 1) = "6"
                            sConvert(23, 2) = "six "
        sConvert(24, 1) = "7"
                            sConvert(24, 2) = "seven "
        sConvert(25, 1) = "8"
                            sConvert(25, 2) = "eight "
        sConvert(26, 1) = "9"
                            sConvert(26, 2) = "nine "
        sConvert(27, 1) = "1"
                            sConvert(27, 2) = "one "
                            
        sConvert(1, 0) = "27"
    
    Return
    
End Function


Download this snippet    Add to My Saved Code

Spells out a numeric value as english words. Comments

No comments have been posted about Spells out a numeric value as english words.. Why not be the first to post a comment about Spells out a numeric value as english words..

Post your comment

Subject:
Message:
0/1000 characters