PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] VB6/ Number to Words / 66 digits-VBForums
Results 1 to 37 of 37

Thread: [RESOLVED] VB6/ Number to Words / 66 digits

  1. #1

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Resolved [RESOLVED] VB6/ Number to Words / 66 digits

    Please send me any feedback...thanks
    Attached Files Attached Files

  2. #2

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    My next challenge is to convert Numbers to VBscript. Why? To learn VBscript.
    It is complex but not too complex to begin with. If you have ideas, please share them
    Thanx

  3. #3
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Before I download this... what EXACTLY does it do? Give an example, please.

  4. #4

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    You enter a number less than 66 digits and it converts it to words...
    With full source code.

  5. #5
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Based on what? How do the numbers relate to the words? A combination of character ascii codes, or... what? I guess what I'm trying to get at, is... what would this be useful for, and how would one use it?

  6. #6
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    That is a whole lot of code for what it does, though. And what exactly is the purpose of the opacity thing, or was that just to try it out or something?

  7. #7

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    The opaque? Just trying something. The code was written in 1985 for an Alpha Micro (Alpha Basic). I converted to Qbasic, and last month VB6

  8. #8
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Interesting. I like the opacity thing. The form loaded with errors due to the missing icon, but it works otherwise.

    The first number I entered was 29.5. Ouch!

    Seems this is an integers-only number converter. I prefer mixed-company numbers myself! Are ya up for the challenge???

  9. #9

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    I did not include the Icon? I'll check that.
    As for for the 29.5, R U up for the challenge ?

  10. #10
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Naw.

    If code has GOTOs in it not involving error handling, I GOTO something else.

    It saves lots of hair-pulling and frustration.

    But thanks anyway.
    Last edited by rjbudz; Jan 26th, 2007 at 05:15 PM.

  11. #11
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Quote Originally Posted by rjbudz
    Naw.

    If code has GOTOs in it not involving error handling, I GOTO something else.

    It saves lots of hair-pulling and frustration.

    But thanks anyway.
    Goto's are generally bad, in my opinion. I rarely(and I mean RARELY) use them. If you can avoid them, do so. And in almost every case, they ARE avoidable. It's just bad programming practice, in my honest opinion.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  12. #12

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    People, I simply converted the code from another OS...
    If you like it, send me a point or two.

    Smiles

  13. #13
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    This thread has really been bugging me. I guess maybe it was the challenge. Anyway, I had to get this code out of my head and into my machine.

    I feel better now.

    I didnít spend much time streamlining the code. Nor was I shooting for elegance. Iím sure it can be fine tuned. I just wanted to get it functional.

    This code does what is advertised, but only up to 28 digits. After that the values automagically convert to scientific notation. I'm sure there is a way past this, and I've set the program up to process values up to 10^100 (a centillion or a googol, depending on who you ask).

    Numbers are infinite, but computer mathematics and my patience are not. There is, thankfully, a limit as to how far one can go with this. Myself, I have no interest in going further than named numbers.

    But I am interested to know whatís wrong, and fixing it.
    VB Code:
    1. ' Returns in english words any decimal value entered up to 28 digits.
    2. ' (Assumes U.S. numbering system)
    3.  
    4. ' You'll need three text boxes:
    5. '    txtEntry
    6. '    txtFormattedEntry
    7. '    txtResult       ' make this one big, with multiLine = True
    8. '  And one buton
    9. '     cmdProcess
    10.  
    11. Option Explicit
    12.  
    13. Private SuffixArray(35) As String
    14. Private Sign As String
    15.  
    16. Private Sub cmdProcess_Click()
    17.  
    18.     Dim DecimalLoc As Integer
    19.     Dim DecimalPart As String
    20.     Dim WholeNumberPart As String
    21.     Dim VerifiedNumber As String
    22.     Dim x As Integer
    23.     Dim y As Integer
    24.     Dim Result As String
    25.     Dim LeftDigit As String
    26.     Dim MiddileDigit As String
    27.     Dim RightDigit As String
    28.    
    29.     ' clear old stuff
    30.     txtResult.Text = ""
    31.     txtFormattedEntry.Text = ""
    32.    
    33.     ' make sure it's a number
    34.     VerifiedNumber = ValidateNumber
    35.    
    36.     ' if it's not a valid number then get out
    37.     If Len(VerifiedNumber) = 0 Then
    38.         MsgBox "Text entry is not numeric.", vbExclamation, ""
    39.         Exit Sub
    40.     End If
    41.    
    42.     ' limit of unknown origin
    43.     If Len(VerifiedNumber) > 28 Then
    44.         MsgBox "Number entered its too large.", vbExclamation, ""
    45.         Exit Sub
    46.     End If
    47.    
    48.     ' number names
    49.     Call InitializeSuffixArray
    50.                
    51.     ' split up number if decimal point
    52.     DecimalLoc = InStr(VerifiedNumber, ".")
    53.     If DecimalLoc > 0 Then
    54.         WholeNumberPart = Left(VerifiedNumber, DecimalLoc - 1)
    55.         DecimalPart = Right(VerifiedNumber, Len(VerifiedNumber) - DecimalLoc)
    56.     Else
    57.         WholeNumberPart = VerifiedNumber
    58.         If Len(WholeNumberPart) = 0 Then Exit Sub
    59.     End If
    60.    
    61.     ' process whole numbers first
    62.     ' pad with zeros (so there will always be a Left, Middle  and Right digit)
    63.     Do Until Len(WholeNumberPart) Mod 3 = 0
    64.         WholeNumberPart = "0" & WholeNumberPart
    65.     Loop
    66.    
    67.     ' determine how many groups there are (for SuffixArray)
    68.     y = Len(WholeNumberPart) / 3
    69.    
    70.     ' determine how many groups we need
    71.     For x = 1 To Len(WholeNumberPart) Step 3
    72.    
    73.         LeftDigit = Mid(WholeNumberPart, x, 1)
    74.         MiddileDigit = Mid(WholeNumberPart, x + 1, 1)
    75.         RightDigit = Mid(WholeNumberPart, x + 2, 1)
    76.        
    77.         If LeftDigit <> "0" Then Result = Result & Ones(LeftDigit) & " Hundred"
    78.        
    79.         If MiddileDigit = 1 Then
    80.             Result = Result & Tens(MiddileDigit & RightDigit)
    81.         Else
    82.             Result = Result & Tens(MiddileDigit)
    83.             Result = Result & Ones(RightDigit)
    84.         End If
    85.    
    86.         Result = Result & SuffixArray(y)
    87.         y = y - 1
    88.     Next
    89.    
    90.     ' now the Decimal part
    91.     If Len(DecimalPart) > 0 Then
    92.         Result = Result & " Point"
    93.         For x = 1 To Len(DecimalPart)
    94.             If Mid(DecimalPart, x, 1) <> "0" Then
    95.                 Result = Result & Ones(Mid(DecimalPart, x, 1))
    96.             Else
    97.                 Result = Result & " Zero"
    98.             End If
    99.         Next
    100.    End If
    101.    
    102.     ' ta da!
    103.     txtResult.Text = Trim(Sign & Result)
    104.    
    105.     ' show the entry properly formatted so the result is easier to verify
    106.     txtFormattedEntry = Sign & Trim(Format(VerifiedNumber, "#,###") & "." & DecimalPart)
    107.    
    108.  End Sub
    109. Private Sub InitializeSuffixArray()
    110. ' Number names from: [url]http://www.unc.edu/~rowlett/units/large.html[/url]
    111.  
    112.     SuffixArray(1) = ""
    113.     SuffixArray(2) = " Thousand"                ' 10^3
    114.     SuffixArray(3) = " Million"                 ' 10^6
    115.     SuffixArray(4) = " Billion"                 ' 10^9
    116.     SuffixArray(5) = " Trillion"                ' 10^12
    117.     SuffixArray(6) = " Quadrillion"             ' 10^15
    118.     SuffixArray(7) = " Quintillion"             ' 10^18
    119.     SuffixArray(8) = " Sextillion"              ' 10^21
    120.     SuffixArray(9) = " Septillion"              ' 10^24
    121.     SuffixArray(10) = " Octillion"              ' 10^27
    122. '    SuffixArray(12) = " Nonillion"              ' 10^30
    123. '    SuffixArray(13) = " Decillion"              ' 10^33
    124. '    SuffixArray(14) = " Undecillion"            ' 10^36
    125. '    SuffixArray(15) = " Duodecillion"           ' 10^39
    126. '    SuffixArray(16) = " Tredecillion"           ' 10^42
    127. '    SuffixArray(17) = " Quattordecillion"       ' 10^45
    128. '    SuffixArray(18) = " Quindecillion"          ' 10^48
    129. '    SuffixArray(19) = " Sexdecillion"           ' 10^51
    130. '    SuffixArray(20) = " Septendecillion"        ' 10^54
    131. '    SuffixArray(21) = " Octodecillion"          ' 10^57
    132. '    SuffixArray(22) = " Novemdecillion"         ' 10^60
    133. '    SuffixArray(23) = " Vigintillion"           ' 10^63
    134. '    SuffixArray(24) = " unvigintillion"         ' 10^66
    135. '    SuffixArray(25) = " duovigintillion"        ' 10^69
    136. '    SuffixArray(26) = " trevigintillion"        ' 10^72
    137. '    SuffixArray(27) = " quattuorvigintillion"   ' 10^75
    138. '    SuffixArray(28) = " quinvigintillion"       ' 10^78
    139. '    SuffixArray(29) = " sexvigintillion"        ' 10^81
    140. '    SuffixArray(30) = " septenvigintillion"     ' 10^84
    141. '    SuffixArray(31) = " octovigintillion"       ' 10^87
    142. '    SuffixArray(32) = " novemvigintillion"      ' 10^90
    143. '    SuffixArray(33) = " trigintillion"          ' 10^93
    144. '    SuffixArray(34) = " untrigintillion"        ' 10^96
    145. '    SuffixArray(35) = " duotrigintillion"       ' 10^99
    146.  
    147. End Sub
    148.    
    149.  Private Function Ones(ByVal Digit As String) As String
    150.  
    151.     Select Case Digit
    152.     Case "0": Ones = ""
    153.     Case "1": Ones = " One"
    154.     Case "2": Ones = " Two"
    155.     Case "3": Ones = " Three"
    156.     Case "4": Ones = " Four"
    157.     Case "5": Ones = " Five"
    158.     Case "6": Ones = " Six"
    159.     Case "7": Ones = " Seven"
    160.     Case "8": Ones = " Eight"
    161.     Case "9": Ones = " Nine"
    162.     End Select
    163.    
    164. End Function
    165.  
    166. Private Function Tens(ByVal Digit As String) As String
    167.     If Len(Digit) = 2 Then
    168.         Select Case Digit
    169.         Case "10": Tens = " Ten"
    170.         Case "11": Tens = " Eleven"
    171.         Case "12": Tens = " Twelve"
    172.         Case "13": Tens = " Thirteen"
    173.         Case "14": Tens = " Fourteen"
    174.         Case "15": Tens = " Fifteen"
    175.         Case "16": Tens = " SixTeen"
    176.         Case "17": Tens = " Seventeen"
    177.         Case "18": Tens = " Eighteen"
    178.         Case "19": Tens = " Nineteen"
    179.         End Select
    180.     Else
    181.         Select Case Digit
    182.         Case "0": Tens = ""
    183.         Case "1": Tens = " Ten"
    184.         Case "2": Tens = " Twenty"
    185.         Case "3": Tens = " Thirty"
    186.         Case "4": Tens = " Fourty"
    187.         Case "5": Tens = " Fifty"
    188.         Case "6": Tens = " Sixy"
    189.         Case "7": Tens = " Seventy"
    190.         Case "8": Tens = " Eighty"
    191.         Case "9": Tens = " Ninety"
    192.         End Select
    193.     End If
    194.  
    195. End Function
    196.  
    197. Private Function ValidateNumber() As String
    198.  
    199.     Dim NextChar As String
    200.     Dim OutputText As String
    201.     Dim FirstDecinalPoint As Boolean
    202.     Dim TrimmedEntry As String
    203.     Dim x As Integer
    204.        
    205.     ' check for negative number
    206.     TrimmedEntry = Trim(txtEntry.Text)
    207.     ' determine positive or negative. Sign is global
    208.      If Left(TrimmedEntry, 1) = "-" Then
    209.         Sign = "Negative"
    210.         TrimmedEntry = Right(TrimmedEntry, Len(TrimmedEntry) - 1)
    211.     Else
    212.         Sign = ""
    213.     End If
    214.    
    215.     ' only one decimal point allowed
    216.     FirstDecinalPoint = True
    217.    
    218.     ' check each character of entry
    219.     For x = 1 To Len(Trim(TrimmedEntry))
    220.         NextChar = Mid(Trim(TrimmedEntry), x, 1)
    221.         If IsNumeric(NextChar) Or (FirstDecinalPoint = True And NextChar = ".") Then
    222.             If NextChar = "." Then FirstDecinalPoint = False
    223.             OutputText = OutputText & NextChar
    224.         Else
    225.             If NextChar <> "," Then
    226.                 ValidateNumber = ""
    227.                 Exit Function
    228.             End If
    229.         End If
    230.     Next
    231.    
    232.    ' remove leading and trailing zeros
    233.    ValidateNumber = Format(OutputText, "General Number")
    234.    
    235. End Function

  14. #14

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    I am just happy that you liked it.
    And I code old school...keep it small

  15. #15
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    The code should not really be all that long, to be honest. The names are the longest part of it, but you could get around that by saving them to a txt file and parsing them that way, rather than creating lines and lines of code. Generally, when I'm working with a structured setting like that(i.e. 10 = "Ten", 11 = "Eleven", or 10^3 = "Thousand", 10^6 = "Million", etc.), I'd create a database file to reference, rather than assigning values or variables for each one and referencing them that way. That's too much work, too many lines of code to keep track of. I'd rather just setup a file, and write a couple of lines to parse it accordingly. As far as searching the string, it would all be a matter of finding the reference point of how many numbers it contains(i.e. thousands, millions, billions, etc.), and converting the numbers to text according to the reference for each in the database.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  16. #16

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    B.D., To much work for this program.
    From your profile, MAC and PC, look at http://www.realbasic.com/
    I Ghosted my PC installed RB,...

  17. #17
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Thanks for your input, guys. In my post, I did state that this was just code to get it to work. Not shooting for any prizes for least lines of code, efficiency or elegance here. Reading from text files (or creating a database to hold names) is beyond the scope and requirements of such a small, trivial application (It's less than 200 lines of code!).

    It's just code to get the job done.

    This type of thing happens a lot in the real world. It's not recommended, but such is life.

    Anyway, I was hoping for some help regarding the unexpected limitations.
    Last edited by rjbudz; Feb 1st, 2007 at 01:57 PM.

  18. #18
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    ... Of course it would be a lot of fun to create a Rube Goldberg* out of it!


    * http://en.wikipedia.org/wiki/Rube_Goldberg_machine

  19. #19
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Ok, I can tell by the vast amount of traffic and replies that this thread has been getting since I posted the new & improved version, and asked for assistance (tap, tap, tap, hello, is this thing on?) that everyone has been waiting with unbearable anticipation to know exactly what was wrong with my code.

    So without further ado: I was converting the input string to a double to use Format(). I tore it out, and now it all works. Enter up to 102 digits into the application and zing-bam-zing you get the English equivalent.

    What could you use it for, you ask?

    Why writing checks of course!

    So, just in case you want to pay off every debt that's ever been made, and any future ones for all mankind, you WILL need this app. Trust me on this!
    Attached Files Attached Files

  20. #20

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    RJ, I know you spent much time on this, and you still wrote in "New School"
    Just append "teen" to the number ...etc
    I am impressed you found numbers beyond 66 digits.
    And if you want some to write some code, look at my "Text to Excel"...I need to add some user controls like ListView...interested?

  21. #21
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    I'll take a look. What's the link?

  22. #22

  23. #23
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    I looked. Please go to that thread and let me know what you need help with!

  24. #24
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    My version, handles the digits in groups of three or power of one thousand.

    NumToWord(ByVal NumStr As String, [KiloDelimiter As String = ","], [Conversion As VbStrConv = vbProperCase]) As String

    NumStr
    - additional validation and string processing
    - converted to integer array in groups of 3 starting with least significant digits

    KiloDelimeter
    - delimeter used on words grouped by power of thousand, default is comma

    Conversion
    - value passed on to StrConv()

    Remarks:
    - Sample implementation handled only converion to words of whole number part, decimal portion handling is left as case to case. Power_ToWord() can be used for negative powers of one thousand (eg. millionth) if you choose to handle the decimal portion that way.
    Attached Files Attached Files

  25. #25

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    One can always make a better mouse trap.
    Like I said, a client wrote this in the eighties (84?) on an Alpha Micro (AlphaBasic).
    While fixing several Y2K clients, I found this code, and converted to Qbasic...fifteen minutes.
    Then last year my niece was asking about numbers, and how big do they become.
    Being the good uncle, I converted it again...thirty minutes.
    http://www.vbforums.com/showthread.php?t=443956
    I had some problems with the Data statements.
    Enjoy

  26. #26
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Oh, I call foul. Digits to the right of zero (& the decimal point) need to be converted to English too!

    I like your approach to the problem, though.
    Last edited by rjbudz; Feb 18th, 2007 at 12:28 AM.

  27. #27
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    I'm not belittling anyone else's approach, in fact I used it as basis in the interest of providing other members with an updated approach.

    As to the decimals, I still believe its case to case... it could be units based such as cents, microns, nanoseconds, etc. The module can be updated to handle such special cases, just create an appropriate procedure and call it. Such as accepting strDecimal as an argument then converting it to microns (up to implementor if the new decimals from shift to microns will be dropped).


    EDIT: I updated the source for converting numbers to their word (or english or spelled) equivalents. Made it leaner and it now uses just three procedures. There is also the option to suppress prompting the user if errors were encountered.

    Attached is the update. Feel free to debug and/or comment.

    EDIT 2008-08-29: Previous update accidentally shifted to Trim instead of LTrim in removing leading zeroes. Code updated. Thanks Keithuk for heads up
    Attached Files Attached Files
    Last edited by leinad31; Aug 29th, 2008 at 08:10 AM.

  28. #28
    New Member
    Join Date
    Oct 2007
    Posts
    5

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    hi i am lokking for a word converter as same like this but i need the decimal also.ex12.5 twelve point five
    or26.25 =twentysix point two five so can u help me?

  29. #29
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Depends on the wording..if you'll use hundreths, etc or just plain word version of digits.

  30. #30

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Scan for the "." first, then pass the second set of numbers

  31. #31
    Lively Member
    Join Date
    Apr 2009
    Posts
    94

    Talking Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Quote Originally Posted by leinad31 View Post
    I'm not belittling anyone else's approach, in fact I used it as basis in the interest of providing other members with an updated approach.

    As to the decimals, I still believe its case to case... it could be units based such as cents, microns, nanoseconds, etc. The module can be updated to handle such special cases, just create an appropriate procedure and call it. Such as accepting strDecimal as an argument then converting it to microns (up to implementor if the new decimals from shift to microns will be dropped).


    EDIT: I updated the source for converting numbers to their word (or english or spelled) equivalents. Made it leaner and it now uses just three procedures. There is also the option to suppress prompting the user if errors were encountered.

    Attached is the update. Feel free to debug and/or comment.

    EDIT 2008-08-29: Previous update accidentally shifted to Trim instead of LTrim in removing leading zeroes. Code updated. Thanks Keithuk for heads up
    the number- 2,59,082
    it shows:- Two Hundred Fifty Nine Thousand, Eighty Two
    How do i convert like this: Two Lakh Fifty Nine thousand, Eighty two only
    Last edited by Eliminator2009; Jul 27th, 2010 at 06:11 AM.
    I belive in pray. I pray for you.
    You may have happy, healthy, wealthy life.

  32. #32
    Addicted Member
    Join Date
    Mar 2007
    Location
    India
    Posts
    227

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Quote Originally Posted by Eliminator2009 View Post
    the number- 2,59,082
    it shows:- Two Hundred Fifty Nine Thousand, Eighty Two
    How do i convert like this: Two Lakh Fifty Nine thousand, Eighty two only
    You need to edit the code yourself.

    This code is made to handle the western numbering convention only.

  33. #33
    New Member
    Join Date
    Aug 2012
    Posts
    1

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    hope this will help you.. for Convert Number to Word (Indian Format)... Full source code Num2Word.zip Here.. You can modify it with what you need.. The Code also cover the Indian Number format issue.. ie. 12,34,563 instead of 123,456,123.

  34. #34

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    I have not coded VB in years, and I only converted this code from an , Alpha Micro 68000 system, Alpha Basic, years before.
    Sorry

  35. #35
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    I didn't write this function, but I did fix some bugs and greatly expand the highest number possible (as well as change data types to handle it, among other things). It currently handles numbers up to 606 digits long (1 duocenuntillion - 1), after that it returns a number in terms of the largest unit in its array. Also I did this over a decade ago when I was a teenager. So go easy on me, I know it needs updating, but I rarely use it and it works as-is.

    Code:
    Public Function ConvertNumberToText(ByVal aNumber As String) As String
    On Error Resume Next
    
    'handles numbers natively from zero all the way through (1 duocenuntillion - 1)
    
    
    'larger numbers are accepted, but are named in the highest available unit
    
    If (aNumber$ = "0") Or (aNumber = "-0") Then
        ConvertNumberToText = "Zero"
        Exit Function
    End If
    Dim bNeg As Boolean
    Dim aNames As Variant
        Dim iNameCounter%, aNumberName$, aTempRet$, bNeedHyphen As Boolean, iTemp%, szTemp As String, szTR As String
    
    aNumber = Replace(aNumber, ",", "") 'no commas in the input, k thx
    aNumber = Replace(aNumber, " ", "") 'or spaces
    
    If Left(aNumber, 1) = "-" Then
        aNumber = Mid$(aNumber, 2)
        bNeg = True
    End If
    
        
        If Len(aNumber) > 606 Then
            szTemp = Left(aNumber, Len(aNumber) - 603)
            aNumber = Right(aNumber, 603)
            szTR = ConvertNumberToText(szTemp)
        End If
        If Len(aNumber) > 3 Then
            aNames = Array("", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", _
                            "Decillion", "Undecillion", "Duodecillion", "Tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septendecillion", "Octodecillion", "Novemdecillion", _
                            "Vigintillion", "Unvigintillion", "Duovigintillion", "Trevigintillion", "Quattuorvigintillion", "Quinvigintillion", "Sexvigintillion", "Septenvigintillion", "Octovigintillion", "Novemvigintillion", _
                            "Trigintillion", "Untrigintillion", "Duotrigintillion", "Tretrigintillion", "Quattuortrigintillion", "Quintrigintillion", "Sextrigintillion", "Septtrigintillion", "Octotrigintillion", "Novemtrigintillion", _
                            "Quadragintillion", "Unquadragintillion", "Duoquadragintillion", "Trequadragintillion", "Quattuorquadragintillion", "Quinquadragintillion", "Sexquadragintillion", "Septquadragintillion", "Octoquadragintillion", "Novemquadragintillion", _
                            "Quinquagintillion", "Unquinquagintillion", "Duoquinquagintillion", "Trequinquagintillion", "Quattuorquinquagintillion", "Quinquinquagintillion", "Sexquinquagintillion", "Septquinquagintillion", "Octoquinquagintillion", "Novemquinquagintillion", _
                            "Sexagintillion", "Unsexagintillion", "Duosexagintillion", "Tresexagintillion", "Quattuorsexagintillion", "Quinsexagintillion", "Sexsexagintillion", "Septsexagintillion", "Octosexagintillion", "Novemsexagintillion", _
                            "Septuagintillion", "Unseptuagintillion", "Duoseptuagintillion", "Treseptuagintillion", "Quattuorseptuagintillion", "Quinseptuagintillion", "Sexseptuagintillion", "Septseptuagintillion", "Octoseptuagintillion", "Novemseptuagintillion", _
                            "Octogintillion", "Unoctogintillion", "Duooctogintillion", "Treoctogintillion", "Quattuoroctogintillion", "Quinoctogintillion", "Sexoctogintillion", "Septoctogintillion", "Octooctogintillion", "Novemoctogintillion", _
                            "Nonagintillion", "Unnonagintillion", "Duononagintillion", "Trenonagintillion", "Quattuornonagintillion", "Quinnonagintillion", "Sexnonagintillion", "Septnonagintillion", "Octononagintillion", "Novemnonagintillion", _
                            "Centillion", "Cenuntillion", "Cenduotillion", "Centretillion", "Cenquattuortillion", "Cenquintillion", "Censextillion", "Censeptentillion", "Cenoctotillion", "Cennovemtillion", _
                            "Cendecillion", "Cenundecillion", "Cenduodecillion", "Centredecillion", "Cenquattuordecillion", "Cenquindecillion", "Censexdecillion", "Censeptendecillion", "Cenoctodecillion", "Cennovemdecillion", _
                            "Cenvigintillion", "Cenunvigintillion", "Cenduovigintillion", "Centrevigintillion", "Cenquattuorvigintillion", "Cenquinvigintillion", "Censexvigintillion", "Censeptenvigintillion", "Cenoctovigintillion", "Cennovemvigintillion", _
                            "Centrigintillion", "Cenuntrigintillion", "Cenduotrigintillion", "Centretrigintillion", "Cenquattuortrigintillion", "Cenquintrigintillion", "Censextrigintillion", "Censeptentrigintillion", "Cenoctotrigintillion", "Cennovemtrigintillion", _
                            "Cenquadragintillion", "Cenunquadragintillion", "Cenduoquadragintillion", "Centrequadragintillion", "Cenquattuorquadragintillion", "Cenquinquadragintillion", "Censexquadragintillion", "Censeptenquadragintillion", "Cenoctoquadragintillion", "Cennovemquadragintillion", _
                            "Cenquinquagintillion", "Cenunquinquagintillion", "Cenduoquinquagintillion", "Centrequinquagintillion", "Cenquattuorquinquagintillion", "Cenquinquinquagintillion", "Censexquinquagintillion", "Censeptenquinquagintillion", "Cenoctoquinquagintillion", "Cennovemquinquagintillion", _
                            "Censexagintillion", "Cenunsexagintillion", "Cenduosexagintillion", "Centresexagintillion", "Cenquattuorsexagintillion", "Cenquinsexagintillion", "Censexsexagintillion", "Censeptensexagintillion", "Cenoctosexagintillion", "Cennovemsexagintillion", _
                            "Censeptuagintillion", "Cenunseptuagintillion", "Cenduoseptuagintillion", "Centreseptuagintillion", "Cenquattuorseptuagintillion", "Cenquinseptuagintillion", "Censexseptuagintillion", "Censeptenseptuagintillion", "Cenoctoseptuagintillion", "Cennovemseptuagintillion", _
                            "Cenoctogintillion", "Cenunoctogintillion", "Cenduooctogintillion", "Centreoctogintillion", "Cenquattuoroctogintillion", "Cenquinoctogintillion", "Censexoctogintillion", "Censeptenoctogintillion", "Cenoctooctogintillion", "Cennovemoctogintillion", _
                            "Cennonagintillion", "Cenunnonagintillion", "Cenduononagintillion", "Centrenonagintillion", "Cenquattuornonagintillion", "Cenquinnonagintillion", "Censexnonagintillion", "Censeptennonagintillion", "Cenoctononagintillion", "Cennovemnonagintillion", _
                            "Duocentillion")
            'The question is how high we should really go...
            aNumber = String$(3 - Len(aNumber) Mod 3, "0") & aNumber
            Do
                aTempRet = ConvertNumberToText(Right$(aNumber, 3))
                If Len(aTempRet) > 0 Then
                    If iNameCounter > 0 Then
    
                            aNumberName = aTempRet & " " & aNames(iNameCounter) & ", " & aNumberName
                        
                    Else
                        aNumberName = aTempRet
                    End If
                End If
                aNumber = Left$(aNumber, Len(aNumber) - 3)
                iNameCounter = iNameCounter + 1
            Loop Until Len(aNumber) = 0
        Else
            aNumber = Trim$(Val(aNumber))
            If Len(aNumber) = 3 Then
                aNumberName = Choose(Val(Left$(aNumber, 1)), "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine") & " Hundred "
                aNumber = Trim$(Val(Right$(aNumber, 2)))
            End If
            If Len(aNumber) = 2 Then
                iTemp = Val(Left$(aNumber, 1))
                If iTemp > 1 Then
                    aNumberName = aNumberName & Choose(iTemp, "", "Twenty", "Thrirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty")
                    aNumber = Trim$(Val(Right$(aNumber, 1)))
                    bNeedHyphen = True
                Else
                    aNumberName = aNumberName & Choose(Val(Right$(aNumber, 1)) + 1, "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
                    aNumber = ""
                End If
            End If
            If Len(aNumber) = 1 Then
                iTemp = Val(Left$(aNumber, 1))
                If iTemp > 0 Then
                    If bNeedHyphen Then aNumberName = aNumberName & "-"
                    aNumberName = aNumberName & Choose(iTemp, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine")
                End If
            End If
        End If
        If szTR <> "" Then
            'szTR = Left(szTR, Len(szTR) - 1) 'trim last comma
            aNumberName = szTR & " Duocentillion, " & aNumberName
        End If
        szTR = RTrim$(aNumberName)
        If Right(szTR, 1) = "," Then
            szTR = Left(szTR, Len(szTR) - 1)
        End If
        If bNeg Then szTR = "Negative " & szTR
        ConvertNumberToText = szTR
    End Function

    ALSO...


    There is an algorithm to name ANY number (>10^3000003; millia is the highest prefix, so it gets to milliamillia...), but it's fairly complex, and the only example I've ever found was written in CGI. It would take far longer than I'm able to spend for me to figure out how to do it in VB, but if someone else was interested, here's what I'm referring to: http://www.isthe.com/chongo/tech/mat...r/howhigh.html for the explanations; and the source of the script: http://www.isthe.com/chongo/tech/math/number/number (working version is on the site).
    If anyone ever converts that algorithm to VB, please PM me!

  36. #36
    New Member
    Join Date
    Nov 2015
    Posts
    1

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    Try this Simple Indian Style Programming (Luv Manish Bhardwaj)
    =================================================

    Private Sub BillSave_Click()
    Dim num As Long 'Number
    Dim length, r, ctr, o, t, h, th, tth, l, tl, c, tc As Integer 'Variables to store o,t,h,th,tth,l,ten lakh etc.

    '================================First Part====================================================
    num = Val(AmountR.Text) 'Take your number
    length = Len(AmountR.Text) 'Find the length
    While num >= 1 'Loop until num is greter or = 1
    ctr = ctr + 1
    r = num Mod 10 'Find the remainder after dividing by 10 so as to get last digit
    If ctr = 1 Then
    o = r 'First value is Once
    ElseIf ctr = 2 Then
    t = r 'Second is Tens
    ElseIf ctr = 3 Then
    h = r 'Third is Hundred
    ElseIf ctr = 4 Then
    th = r
    ElseIf ctr = 5 Then
    tth = r
    ElseIf ctr = 6 Then
    l = r
    ElseIf ctr = 7 Then
    tl = r
    ElseIf ctr = 8 Then
    c = r
    ElseIf ctr = 9 Then
    tc = r 'Until Ten Crore
    End If
    num = Fix(num / 10) 'new number is except last digit eg. if num is 1234 here num will be 123
    Wend

    '================================Second Part====================================================
    Dim str As String
    Dim res As String
    str = ""
    res = ""
    While length >= 1 'Loop until all the digits
    If length = 9 And tc > 1 Then
    res = findfig(tc * 10) 'Calling function to find TenCrore's WordValue Multiply by 10 to get ten, twenty
    str = str + res 'Adding result into a string
    ElseIf length = 8 Then
    If tc = 1 Then
    res = findfig(c + 10) 'Adding Ten to crore value to get 11,12,13 etc
    Else
    res = findfig(c)
    End If
    If tc = 0 And c = 0 Then 'Display when tc and c both are not zero
    Else
    str = str + res + " Crore "
    End If
    ElseIf length = 7 And tl > 1 Then
    res = findfig(tl * 10)
    str = str + res
    ElseIf length = 6 Then
    If tl = 1 Then
    res = findfig(l + 10)
    Else
    res = findfig(l)
    End If
    If tl = 0 And l = 0 Then
    Else
    str = str + res + " Lakh "
    End If
    ElseIf length = 5 And tth > 1 Then
    res = findfig(tth * 10)
    str = str + res
    ElseIf length = 4 Then
    If tth = 1 Then
    res = findfig(th + 10)
    Else
    res = findfig(th)
    End If
    If tth = 0 And th = 0 Then
    Else
    str = str + res + " Thousands "
    End If
    ElseIf length = 3 Then
    res = findfig(h)
    If Not h = 0 Then
    str = str + res + " Hundred "
    End If
    ElseIf length = 2 And t > 1 Then
    res = findfig(t * 10)
    str = str + res
    ElseIf length = 1 Then
    If t = 1 Then
    res = findfig(o + 10)
    Else
    res = findfig(o)
    End If
    str = str + res + " Only"
    End If
    length = length - 1
    Wend
    AmtWords.Caption = str 'Use str as final Result
    End Sub

    Public Function findfig(ByVal length As Integer) As String
    Dim str As String
    str = ""
    Select Case length

    Case 1
    str = "One"
    Case 2
    str = "Two"
    Case 3
    str = "Three"
    Case 4
    str = "Four"
    Case 5
    str = "Five"
    Case 6
    str = "Six"
    Case 7
    str = "Seven"
    Case 8
    str = "Eight"
    Case 9
    str = "Nine"
    Case 10
    str = "Ten"
    Case 11
    str = "Eleven"
    Case 12
    str = "Twelve"
    Case 13
    str = "Thirteen"
    Case 14
    str = "Fourteen"
    Case 15
    str = "Fifteen"
    Case 16
    str = "Sixteen"
    Case 17
    str = "Seventeen"
    Case 18
    str = "Eightteen"
    Case 19
    str = "Nineteen"
    Case 20
    str = "Twenty "
    Case 30
    str = "Thirty "
    Case 40
    str = "Forty "
    Case 50
    str = "Fifty "
    Case 60
    str = "Sixty "
    Case 70
    str = "Seventy "
    Case 80
    str = "Eighty "
    Case 90
    str = "Ninety "
    End Select
    findfig = str
    End Function

  37. #37

    Thread Starter
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    952

    Re: [RESOLVED] VB6/ Number to Words / 66 digits

    manishxy , there is always room for improvement, like I posted, this was written in, Alpha Basic on an Alpha Micro system in 1984?
    Then converted to Qbasic, thank for looking it up....smiles

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width