Results 1 to 6 of 6

Thread: Need Troubleshooting Help [resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492

    Resolved Need Troubleshooting Help [resolved]

    I have an application that changes and $$ amount into a written out ammount. Ex. 250$ = Two Hundred Fifty Dollars. I have the following code that i want to modify, but i have found that it doesnt correctly translate the cents part, can you guys help me out on this one and tell me what is causing this, it doesnt always translate incorrectly only on certain occasions. I only have a text box and a command button so it should be easy to set up.

    Example: 243.42 = Two hundres forty three dollars and forty one cents
    another example: 96.86 = Ninety Six Dollars and Eighty Five cents
    while when you do 6.32 it comes up correctly. What is causing this cause i cant find it.

    Thanks for you help in advance.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim Arr1 As Variant
    4. Dim Arr10 As Variant
    5. Dim Arr100 As Variant
    6.  
    7. Private Sub GetWords(Amt As Currency)
    8. Dim d1 As Long
    9. Dim d10 As Long
    10. Dim d100 As Long
    11. Dim d1000 As Long
    12. Dim d100000 As Long
    13. Dim d1000000 As Long
    14. Dim c1 As Long
    15. Dim c10 As Long
    16. Dim Words As String
    17. Dim Amount As Double
    18. Dim tmpAmt As Double
    19.  
    20.  
    21. '///////////////////////////
    22. ' Dollar Processing
    23. '///////////////////////////
    24.  
    25. Amount = Amt
    26.  
    27. d1000000 = Int(Amount / 1000000): Amount = Amount - (d1000000 * 1000000)
    28. d100000 = Int(Amount / 100000): Amount = Amount - (d100000 * 100000)
    29. d1000 = Int(Amount / 1000): Amount = Amount - (d1000 * 1000)
    30. d100 = Int(Amount / 100): Amount = Amount - CDbl(d100 * 100)
    31. d10 = Int(Amount / 10): Amount = Amount - CDbl(d10 * 10)
    32. d1 = Int(Amount): Amount = Amount - d1
    33.  
    34. '///////////////////////////
    35. ' Cents Processing
    36. '///////////////////////////
    37.  
    38. Amount = Amount * 100
    39. c10 = Int(Amount / 10): Amount = Amount - (c10 * 10)
    40. c1 = Int(Amount): Amount = Amount - c1
    41.  
    42. Words = ""
    43.  
    44. '///////////////////////////
    45. ' Dollars Words Processing
    46. '///////////////////////////
    47.  
    48. If d1000000 > 0 Then
    49.    If d1000000 < 19 Then
    50.        Amount = d1000000
    51.        Words = Words & Arr1(d1000000) & " Million "
    52.     Else
    53.     If d1000000 > 19 Then
    54.        Amount = d1000000
    55.        Words = Words & Arr100(Int(d1000000 / 10)): Amount = (d1000000 Mod 10)
    56.        If Amount > 0 Then Words = Words & " " & Arr1(Amount)
    57.        Words = Words & " Million "
    58.     Else
    59.        If d1000 = 0 Then Words = Words & Arr1(d1000000) & " Million "
    60.     End If
    61. End If
    62. End If
    63. If d100000 > 0 Then
    64.     Words = Words & GetHundreds(Int(d100000 / 10)): Amount = (d100000 Mod 10)
    65.     If (Amount > 0 And d1000 = 0) Then
    66.         Words = Words & Arr1(d100000) & " Hundred Thousand "
    67.     Else
    68.     If d1000 > 0 Then Words = Words & Arr1(d100000) & " Hundred "
    69.    
    70.     End If
    71. End If
    72.  
    73. If d1000 > 19 Then
    74.    Amount = d1000
    75.    Words = Words & Arr100(Int(d1000 / 10)): Amount = (d1000 Mod 10)
    76.    If Amount > 0 Then Words = Words & " " & Arr1(Amount)
    77.    Words = Words & " Thousand "
    78. Else
    79.    If d1000 > 0 Then Words = Words & Arr1(d1000) & " Thousand "
    80. End If
    81.  
    82. Words = Words & GetHundreds(d100)
    83. Words = Words & GetTens(d1, d10, " Dollars")
    84.  
    85. '///////////////////////////
    86. ' Cents Words Processing
    87. '///////////////////////////
    88.  
    89. Words = Words & " and " & GetCents(c1, c10, " Cents")
    90. MsgBox Words
    91. Text1.Text = ""
    92.  
    93.  
    94. End Sub
    95. Function GetTens(iones As Long, itens As Long, stype As String) As String
    96. Dim ones, tens As Integer
    97.  
    98. ones = iones
    99. tens = itens
    100.  
    101.     If ones > 0 And tens = 1 Then
    102.        GetTens = GetTens & Arr1(ones + (tens * 10)) & " "
    103.        ones = 0
    104.     Else
    105.        GetTens = GetTens & Arr100(tens) & " "
    106.     End If
    107.    
    108.     If ones > 0 Then
    109.        GetTens = GetTens & Arr1(ones) & stype
    110.     Else
    111.        GetTens = stype
    112.     End If
    113.  
    114. End Function
    115. Function GetCents(iones As Long, itens As Long, stype As String) As String
    116. Dim ones, tens As Integer
    117.  
    118. ones = iones
    119. tens = itens
    120.  
    121.     If ones > 0 And tens = 1 Then
    122.        GetCents = GetCents & Arr1(ones + (tens * 10)) & " "
    123.        ones = 0
    124.     Else
    125.        GetCents = GetCents & Arr100(tens) & " "
    126.     End If
    127.    
    128.     If ones > 0 Then
    129.        GetCents = GetCents & Arr1(ones) & stype
    130.     Else
    131.        GetCents = "Zero" & stype
    132.     End If
    133.  
    134. End Function
    135.  
    136. Function GetHundreds(iHundreds As Long) As String
    137.     If iHundreds > 0 Then GetHundreds = Arr1(iHundreds) & " Hundred "
    138. End Function
    139. Function GetHundredsThs(iHundreds As Long) As String
    140.     If iHundreds > 0 Then GetHundredsThs = Arr1(iHundreds) & " Hundred "
    141. End Function
    142.  
    143. Private Sub Command1_Click()
    144.    GetWords CCur(Val(Text1.Text))
    145. End Sub
    146.  
    147. Private Sub Form_Load()
    148.  
    149. Arr1 = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", _
    150.               "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", _
    151.               "Seventeen", "Eighteen", "Ninteen")
    152. Arr10 = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine")
    153. Arr100 = Array("", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
    154.  
    155. End Sub
    Last edited by mrstuff68; Oct 10th, 2005 at 11:39 AM.

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: Need Troubleshooting Help

    I have a sample app on my site that converts a number to words. It is implemented as a function "Num2Words". It only works on integers. To do what you want, you will need to call the function twice. Break up your string at the decimal point (you can use Split or other string-handling functions to do this), then call the routine once passing it the "dollars" portion and append the text " dollars and ", then call it again passing it the "cents" portion, and append that result along with the word "cents" to the final result.

    The code can be found here:
    http://www.thevbprogrammer.com/index...ter=8&Topic=11
    "It's cold gin time again ..."

    Check out my website here.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492

    Re: Need Troubleshooting Help

    BruceG, I will look into your app, but i would still like to know what is causing the code that i currently have to act the way it is. If it was happening all the time then i would know that there is something wrong with the code but if it acts like that on certain instances, i would really like to know what is causing it.

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Need Troubleshooting Help

    You are using LONG's which don't handle division well. The drawback to using SINGLE is that there is a range limit of 32768. You could use Currency type though, which should be what you need. Change the type of C1 and C10 for sure, to solve your problem.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492

    Re: Need Troubleshooting Help

    I knew it was something small.

    Thanks dglienna

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Need Troubleshooting Help [resolved]

    Ever since I discovered CHOOSE function, it has made my functions very small in cases like this one.
    VB Code:
    1. Private Sub Command1_Click()
    2.     '*** Usage ***
    3.     MsgBox AmountToWords(InputBox("Enter Amount:"))
    4. End Sub
    5.  
    6. Private Function AmountToWords(ByVal num As Double)
    7.     Dim s As String, sa() As String
    8.    
    9.     'Rupees part
    10.     s = Int(num)
    11.     s = Format(s, "-##-##-##-##-##-#-##")
    12.     Do While InStr(s, "--") > 0
    13.         s = Replace(s, "--", "-")   'remove extra seperators
    14.     Loop
    15.     If s = "-" Then
    16.         AmountToWords = "Rupees Zero"
    17.     Else
    18.         sa = Split(s, "-")
    19.         For i = UBound(sa) To 1 Step -1
    20.             AmountToWords = GetNum(sa(i)) & GetPlace(sa(i), UBound(sa) - i + 1) & AmountToWords
    21.         Next
    22.         AmountToWords = "Rupees " & AmountToWords
    23.     End If
    24.    
    25.     'Paise Part
    26.     If Int(num) <> num Then
    27.         s = Format(num, "0.00")
    28.         s = Mid(s, InStr(s, ".") + 1, 2)
    29.         AmountToWords = AmountToWords & " and " & GetNum(s) & " paise"
    30.     End If
    31.     AmountToWords = AmountToWords & " only"
    32. End Function
    33.  
    34. Private Function GetNum(ByVal num As Long) As String
    35.     Select Case num
    36.     Case 0
    37. '        GetNum = "zero"
    38.     Case Is < 11
    39.         GetNum = Choose(num, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
    40.     Case Is < 20
    41.         GetNum = Choose(num - 10, "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
    42.     Case Is < 100
    43.         GetNum = Choose(num \ 10, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
    44.         GetNum = GetNum & " " & GetNum(num Mod 10)
    45.     End Select
    46. End Function
    47.  
    48. Private Function GetPlace(ByVal num As Long, ByVal nPlace As Long) As String
    49.     If num > 0 Then GetPlace = Choose(nPlace, "", " hundred ", " thousand ", " lakh ", " crore ", " million ", " billion ", " trillion ")
    50. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

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



Click Here to Expand Forum to Full Width