To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Oct 10th, 2005, 11:15 AM   #1
mrstuff68
Hyperactive Member
 
Join Date: Apr 02
Posts: 492
mrstuff68 is an unknown quantity at this point (<10)
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. Dim Arr1 As Variant
  3. Dim Arr10 As Variant
  4. Dim Arr100 As Variant
  5. Private Sub GetWords(Amt As Currency)
  6. Dim d1 As Long
  7. Dim d10 As Long
  8. Dim d100 As Long
  9. Dim d1000 As Long
  10. Dim d100000 As Long
  11. Dim d1000000 As Long
  12. Dim c1 As Long
  13. Dim c10 As Long
  14. Dim Words As String
  15. Dim Amount As Double
  16. Dim tmpAmt As Double
  17. '///////////////////////////
  18. ' Dollar Processing
  19. '///////////////////////////
  20. Amount = Amt
  21. d1000000 = Int(Amount / 1000000): Amount = Amount - (d1000000 * 1000000)
  22. d100000 = Int(Amount / 100000): Amount = Amount - (d100000 * 100000)
  23. d1000 = Int(Amount / 1000): Amount = Amount - (d1000 * 1000)
  24. d100 = Int(Amount / 100): Amount = Amount - CDbl(d100 * 100)
  25. d10 = Int(Amount / 10): Amount = Amount - CDbl(d10 * 10)
  26. d1 = Int(Amount): Amount = Amount - d1
  27. '///////////////////////////
  28. ' Cents Processing
  29. '///////////////////////////
  30. Amount = Amount * 100
  31. c10 = Int(Amount / 10): Amount = Amount - (c10 * 10)
  32. c1 = Int(Amount): Amount = Amount - c1
  33. Words = ""
  34. '///////////////////////////
  35. ' Dollars Words Processing
  36. '///////////////////////////
  37. If d1000000 > 0 Then
  38.    If d1000000 < 19 Then
  39.        Amount = d1000000
  40.        Words = Words & Arr1(d1000000) & " Million "
  41.     Else
  42.     If d1000000 > 19 Then
  43.        Amount = d1000000
  44.        Words = Words & Arr100(Int(d1000000 / 10)): Amount = (d1000000 Mod 10)
  45.        If Amount > 0 Then Words = Words & " " & Arr1(Amount)
  46.        Words = Words & " Million "
  47.     Else
  48.        If d1000 = 0 Then Words = Words & Arr1(d1000000) & " Million "
  49.     End If
  50. End If
  51. End If
  52. If d100000 > 0 Then
  53.     Words = Words & GetHundreds(Int(d100000 / 10)): Amount = (d100000 Mod 10)
  54.     If (Amount > 0 And d1000 = 0) Then
  55.         Words = Words & Arr1(d100000) & " Hundred Thousand "
  56.     Else
  57.     If d1000 > 0 Then Words = Words & Arr1(d100000) & " Hundred "
  58.    
  59.     End If
  60. End If
  61. If d1000 > 19 Then
  62.    Amount = d1000
  63.    Words = Words & Arr100(Int(d1000 / 10)): Amount = (d1000 Mod 10)
  64.    If Amount > 0 Then Words = Words & " " & Arr1(Amount)
  65.    Words = Words & " Thousand "
  66. Else
  67.    If d1000 > 0 Then Words = Words & Arr1(d1000) & " Thousand "
  68. End If
  69. Words = Words & GetHundreds(d100)
  70. Words = Words & GetTens(d1, d10, " Dollars")
  71. '///////////////////////////
  72. ' Cents Words Processing
  73. '///////////////////////////
  74. Words = Words & " and " & GetCents(c1, c10, " Cents")
  75. MsgBox Words
  76. Text1.Text = ""
  77. End Sub
  78. Function GetTens(iones As Long, itens As Long, stype As String) As String
  79. Dim ones, tens As Integer
  80. ones = iones
  81. tens = itens
  82.     If ones > 0 And tens = 1 Then
  83.        GetTens = GetTens & Arr1(ones + (tens * 10)) & " "
  84.        ones = 0
  85.     Else
  86.        GetTens = GetTens & Arr100(tens) & " "
  87.     End If
  88.    
  89.     If ones > 0 Then
  90.        GetTens = GetTens & Arr1(ones) & stype
  91.     Else
  92.        GetTens = stype
  93.     End If
  94. End Function
  95. Function GetCents(iones As Long, itens As Long, stype As String) As String
  96. Dim ones, tens As Integer
  97. ones = iones
  98. tens = itens
  99.     If ones > 0 And tens = 1 Then
  100.        GetCents = GetCents & Arr1(ones + (tens * 10)) & " "
  101.        ones = 0
  102.     Else
  103.        GetCents = GetCents & Arr100(tens) & " "
  104.     End If
  105.    
  106.     If ones > 0 Then
  107.        GetCents = GetCents & Arr1(ones) & stype
  108.     Else
  109.        GetCents = "Zero" & stype
  110.     End If
  111. End Function
  112. Function GetHundreds(iHundreds As Long) As String
  113.     If iHundreds > 0 Then GetHundreds = Arr1(iHundreds) & " Hundred "
  114. End Function
  115. Function GetHundredsThs(iHundreds As Long) As String
  116.     If iHundreds > 0 Then GetHundredsThs = Arr1(iHundreds) & " Hundred "
  117. End Function
  118. Private Sub Command1_Click()
  119.    GetWords CCur(Val(Text1.Text))
  120. End Sub
  121. Private Sub Form_Load()
  122. Arr1 = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", _
  123.               "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", _
  124.               "Seventeen", "Eighteen", "Ninteen")
  125. Arr10 = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine")
  126. Arr100 = Array("", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
  127. End Sub

Last edited by mrstuff68; Oct 10th, 2005 at 12:39 PM.
mrstuff68 is offline   Reply With Quote
Old Oct 10th, 2005, 11:27 AM   #2
BruceG
PowerPoster
 
BruceG's Avatar
 
Join Date: May 00
Location: New Jersey (USA)
Posts: 2,543
BruceG is a jewel in the rough (200+)BruceG is a jewel in the rough (200+)BruceG is a jewel in the rough (200+)
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.
BruceG is offline   Reply With Quote
Old Oct 10th, 2005, 12:27 PM   #3
mrstuff68
Hyperactive Member
 
Join Date: Apr 02
Posts: 492
mrstuff68 is an unknown quantity at this point (<10)
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.
mrstuff68 is offline   Reply With Quote
Old Oct 10th, 2005, 12:34 PM   #4
dglienna
Banned
 
dglienna's Avatar
 
Join Date: Jun 04
Location: Center of it all
Posts: 17,901
dglienna is a glorious beacon of light (400+)dglienna is a glorious beacon of light (400+)dglienna is a glorious beacon of light (400+)dglienna is a glorious beacon of light (400+)dglienna is a glorious beacon of light (400+)dglienna is a glorious beacon of light (400+)
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.
dglienna is offline   Reply With Quote
Old Oct 10th, 2005, 12:39 PM   #5
mrstuff68
Hyperactive Member
 
Join Date: Apr 02
Posts: 492
mrstuff68 is an unknown quantity at this point (<10)
Re: Need Troubleshooting Help

I knew it was something small.

Thanks dglienna
mrstuff68 is offline   Reply With Quote
Old Oct 10th, 2005, 02:51 PM   #6
Pradeep1210
Power Poster
 
Pradeep1210's Avatar
 
Join Date: Apr 04
Location: Inside the CPU...
Posts: 4,301
Pradeep1210 is a splendid one to behold (700+)Pradeep1210 is a splendid one to behold (700+)Pradeep1210 is a splendid one to behold (700+)Pradeep1210 is a splendid one to behold (700+)Pradeep1210 is a splendid one to behold (700+)Pradeep1210 is a splendid one to behold (700+)Pradeep1210 is a splendid one to behold (700+)
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. Private Function AmountToWords(ByVal num As Double)
  6.     Dim s As String, sa() As String
  7.    
  8.     'Rupees part
  9.     s = Int(num)
  10.     s = Format(s, "-##-##-##-##-##-#-##")
  11.     Do While InStr(s, "--") > 0
  12.         s = Replace(s, "--", "-")   'remove extra seperators
  13.     Loop
  14.     If s = "-" Then
  15.         AmountToWords = "Rupees Zero"
  16.     Else
  17.         sa = Split(s, "-")
  18.         For i = UBound(sa) To 1 Step -1
  19.             AmountToWords = GetNum(sa(i)) & GetPlace(sa(i), UBound(sa) - i + 1) & AmountToWords
  20.         Next
  21.         AmountToWords = "Rupees " & AmountToWords
  22.     End If
  23.    
  24.     'Paise Part
  25.     If Int(num) <> num Then
  26.         s = Format(num, "0.00")
  27.         s = Mid(s, InStr(s, ".") + 1, 2)
  28.         AmountToWords = AmountToWords & " and " & GetNum(s) & " paise"
  29.     End If
  30.     AmountToWords = AmountToWords & " only"
  31. End Function
  32. Private Function GetNum(ByVal num As Long) As String
  33.     Select Case num
  34.     Case 0
  35. '        GetNum = "zero"
  36.     Case Is < 11
  37.         GetNum = Choose(num, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
  38.     Case Is < 20
  39.         GetNum = Choose(num - 10, "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
  40.     Case Is < 100
  41.         GetNum = Choose(num \ 10, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
  42.         GetNum = GetNum & " " & GetNum(num Mod 10)
  43.     End Select
  44. End Function
  45. Private Function GetPlace(ByVal num As Long, ByVal nPlace As Long) As String
  46.     If num > 0 Then GetPlace = Choose(nPlace, "", " hundred ", " thousand ", " lakh ", " crore ", " million ", " billion ", " trillion ")
  47. End Function
__________________
Pradeep
If anyones answer has helped you please show your appreciation by rating that answer. Just click icon on the left of the post you wish to rate.

My Blog | 101 LINQ Samples | JSON Validator | XML Schema Validator | String Enum | "How Do I" videos on MSDN | VB.NET and C# Comparison

Some Signatures that contain Useful Stuff:
jmcilhinney | szlamany | MartinLiss | RobDog888 | dee-u | RhinoBull | si_the_geek | koolsid | Negative0 | VBDT | gep13 | NickThissen...
Pradeep1210 is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:21 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.