473,414 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

converting numbers to currency?

8
i have been trying to get this code to work but keep getting errors. what am I doing wrong? the errors that I am getting are:

'Public Property Left() As Integer' has no parameters and its return type cannot be indexed.

'Public ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed.


Expand|Select|Wrap|Line Numbers
  1. Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String
  2. Dim Temp As String
  3. Dim Dollars, Cents As String
  4. Dim DecimalPlace, Count As Integer
  5. Dim Place(9) As String
  6. Dim Numb As String
  7.  
  8.     Place(2) = " Thousand ": Place(3) = " Million ": Place(4) = "Billion"
  9.  
  10. : Place(5) = " Trillion "
  11.  
  12.     ' Convert Numb to a string, trimming extra spaces.
  13.     Numb = Trim(Str(MyNumber))
  14.     ' Find decimal place.
  15.     DecimalPlace = InStr(Numb, ".")
  16.     ' If we find decimal place...
  17.     If DecimalPlace > 0 Then
  18.         ' Convert cents
  19.         Temp = Left(Mid(Numb, DecimalPlace + 1) & "00", 2)
  20.         Cents = ConvertTens(Temp)
  21.         ' Strip off cents from remainder to convert.
  22.         Numb = Trim(Left(Numb, DecimalPlace - 1))
  23.     End If
  24.     Count = 1
  25.     Do While Numb <> ""
  26.         ' Convert last 3 digits of Numb to English dollars.
  27.         Temp = ConvertHundreds(Right(Numb, 3))
  28.         If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
  29.         If Len(Numb) > 3 Then
  30.             ' Remove last 3 converted digits from Numb.
  31.             Numb = Left(Numb, Len(Numb) - 3)
  32.             Else
  33.             Numb = ""
  34.         End If
  35.         Count = Count + 1
  36.     Loop
  37.  
  38.     ' Clean up dollars.
  39.     Select Case Dollars
  40.     Case "": Dollars = "No Dollars"
  41.     Case "One": Dollars = "One Dollar"
  42.     Case Else: Dollars = Dollars & " Dollars"
  43.     End Select
  44.  
  45.     ' Clean up cents.
  46.     Select Case Cents
  47.     Case "": Cents = " And No Cents"
  48.     Case "One": Cents = " And One Cent"
  49.     Case Else: Cents = " And " & Cents & " Cents"
  50.     End Select
  51.  
  52.     ConvertCurrencyToEnglish = Dollars & Cents
  53.  
  54. End Function
  55.  
  56. Private Function ConvertHundreds(ByVal MyNumber As String) As String
  57. Dim Result As String
  58.  
  59.     ' Exit if there is nothing to convert.
  60.     If Val(MyNumber) = 0 Then Exit Function
  61.  
  62.     ' Append leading zeros to number.
  63.     MyNumber = Right("000" & MyNumber, 3)
  64.     ' Do we have a hundreds place digit to convert?
  65.     If Left(MyNumber, 1) <> "0" Then
  66.         Result = ConvertDigit(Left(MyNumber, 1)) & " Hundred "
  67.     End If
  68.     ' Do we have a tens place digit to convert?
  69.     If Mid(MyNumber, 2, 1) <> "0" Then
  70.         Result = Result & ConvertTens(Mid(MyNumber, 2))
  71.     Else
  72.         ' If not, then convert the ones place digit.
  73.         Result = Result & ConvertDigit(Mid(MyNumber, 3))
  74.     End If
  75.     ConvertHundreds = Trim(Result)
  76.  
  77. End Function
  78.  
  79. Private Function ConvertTens(ByVal MyTens As String) As String
  80. Dim Result As String
  81.  
  82.     ' Is value between 10 and 19?
  83.     If (MyTens, 1)) = 1 Then
  84.         Select Case Val(MyTens)
  85.         Case 10: Result = "Ten"
  86.         Case 11: Result = "Eleven"
  87.         Case 12: Result = "Twelve"
  88.         Case 13: Result = "Thirteen"
  89.         Case 14: Result = "Fourteen"
  90.         Case 15: Result = "Fifteen"
  91.         Case 16: Result = "Sixteen"
  92.         Case 17: Result = "Seventeen"
  93.         Case 18: Result = "Eighteen"
  94.         Case 19: Result = "Nineteen"
  95.         Case Else
  96.         End Select
  97.     Else
  98.         ' .. otherwise it's between 20 and 99.
  99.         Select Case (MyTens, 1)
  100.         Case 2: Result = "Twenty "
  101.         Case 3: Result = "Thirty "
  102.         Case 4: Result = "Forty "
  103.         Case 5: Result = "Fifty "
  104.         Case 6: Result = "Sixty "
  105.         Case 7: Result = "Seventy "
  106.         Case 8: Result = "Eighty "
  107.         Case 9: Result = "Ninety "
  108.         Case Else
  109.         End Select
  110.         ' Convert ones place digit.
  111.         Result = Result & ConvertDigit(Right(MyTens, 1))
  112.     End If
  113.     ConvertTens = Result
  114.  
  115. End Function
  116.  
  117. Private Function ConvertDigit(ByVal MyDigit As String) As String
  118.  
  119.     Select Case Val(MyDigit)
  120.     Case 1: ConvertDigit = "One"
  121.     Case 2: ConvertDigit = "Two"
  122.     Case 3: ConvertDigit = "Three"
  123.     Case 4: ConvertDigit = "Four"
  124.     Case 5: ConvertDigit = "Five"
  125.     Case 6: ConvertDigit = "Six"
  126.     Case 7: ConvertDigit = "Seven"
  127.     Case 8: ConvertDigit = "Eight"
  128.     Case 9: ConvertDigit = "Nine"
  129.     Case Else: ConvertDigit = ""
  130.     End Select
  131.  
  132. End Function
  133.  
Jul 17 '07 #1
9 2283
MMcCarthy
14,534 Expert Mod 8TB
You don't say whether this is VB or VBA. I am moving it to the VB forum for the moment.

Also what are you trying to do here

Expand|Select|Wrap|Line Numbers
  1.              Temp = Left(Mid(Numb, DecimalPlace + 1) & "00", 2)
and here

Expand|Select|Wrap|Line Numbers
  1. MyNumber = Right("000" & MyNumber, 3)
This doesn't make any sense.
Jul 17 '07 #2
MMcCarthy
14,534 Expert Mod 8TB
Also there is a problem with this function

Expand|Select|Wrap|Line Numbers
  1. Private Function ConvertTens(ByVal MyTens As String) As String
  2. Dim Result As String
  3.  
  4.     ' Is value between 10 and 19?
  5.     If (MyTens, 1)) = 1 Then ' what are you trying to do here ???
  6.         Select Case Val(MyTens)
  7.         Case 10: Result = "Ten"
  8.         Case 11: Result = "Eleven"
  9.         Case 12: Result = "Twelve"
  10.         Case 13: Result = "Thirteen"
  11.         Case 14: Result = "Fourteen"
  12.         Case 15: Result = "Fifteen"
  13.         Case 16: Result = "Sixteen"
  14.         Case 17: Result = "Seventeen"
  15.         Case 18: Result = "Eighteen"
  16.         Case 19: Result = "Nineteen"
  17.         Case Else
  18.         End Select
  19.     Else
  20.         ' .. otherwise it's between 20 and 99.
  21.         Select Case (MyTens, 1) ' and here as well ???
  22.         Case 2: Result = "Twenty "
  23.         Case 3: Result = "Thirty "
  24.         Case 4: Result = "Forty "
  25.         Case 5: Result = "Fifty "
  26.         Case 6: Result = "Sixty "
  27.         Case 7: Result = "Seventy "
  28.         Case 8: Result = "Eighty "
  29.         Case 9: Result = "Ninety "
  30.         Case Else
  31.         End Select
  32.         ' Convert ones place digit.
  33.         Result = Result & ConvertDigit(Right(MyTens, 1))
  34.     End If
  35.     ConvertTens = Result
  36.  
  37. End Function
  38.  
Jul 17 '07 #3
WyattM
8
I'm sorry I am using vb. net 2005. I am trying to enter the amount in numbers on one form and have it appear converted into words in a textbox or label on the second form like when you write a check. If there are any easier ways please feel free to clarify for me.
Jul 17 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
I'm sorry I am using vb. net 2005
OK you are in the right forum now. Have a look at the issues I pointed out.
Jul 17 '07 #5
WyattM
8
Expand|Select|Wrap|Line Numbers
  1. Private Function ConvertTens(ByVal MyTens As String) As String
  2. Dim Result As String
  3.  
  4.     ' Is value between 10 and 19?
  5.     If (MyTens, 1)) = 1 Then ' what are you trying to do here ???( this is where i'm trying to convert the numbers 10 to 19 into words)
  6.         Select Case Val(MyTens)
  7.         Case 10: Result = "Ten"
  8.         Case 11: Result = "Eleven"
  9.         Case 12: Result = "Twelve"
  10.         Case 13: Result = "Thirteen"
  11.         Case 14: Result = "Fourteen"
  12.         Case 15: Result = "Fifteen"
  13.         Case 16: Result = "Sixteen"
  14.         Case 17: Result = "Seventeen"
  15.         Case 18: Result = "Eighteen"
  16.         Case 19: Result = "Nineteen"
  17.         Case Else
  18.         End Select
  19.     Else
  20.         ' .. otherwise it's between 20 and 99.
  21.         Select Case (MyTens, 1) ' and here as well ???(trying to convert the other numbers)
  22.         Case 2: Result = "Twenty "
  23.         Case 3: Result = "Thirty "
  24.         Case 4: Result = "Forty "
  25.         Case 5: Result = "Fifty "
  26.         Case 6: Result = "Sixty "
  27.         Case 7: Result = "Seventy "
  28.         Case 8: Result = "Eighty "
  29.         Case 9: Result = "Ninety "
  30.         Case Else
  31.         End Select
  32.         ' Convert ones place digit.
  33.         Result = Result & ConvertDigit(Right(MyTens, 1))
  34.     End If
  35.     ConvertTens = Result
  36.  
  37. End Function
Jul 17 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
Sorry to be more specific

What is this statement?

If (MyTens, 1)) = 1 Then
Jul 17 '07 #7
WyattM
8
If (MyTens, 1)) = 1 Then is to covert the tens place of the number
Jul 17 '07 #8
MMcCarthy
14,534 Expert Mod 8TB
If (MyTens, 1)) = 1 Then is to covert the tens place of the number
Its not doing that. You need to isolate the number as follows:

Expand|Select|Wrap|Line Numbers
  1. Dim myNum As Integer
  2.  
  3.     myNum = CInt(Right(MyTens, 2)) ' myNum now holds a two digit number
  4.     If  myNum > 9 And myNum < 20 Then 
  5.  
  6.  
Jul 17 '07 #9
WyattM
8
ok will try that thank you.
Jul 17 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: PoulsenL | last post by:
I have a number of text fields with different ways of exressing currency. Examples: 1 512,3 M 0.214,0 m 1 123 k 1,234,567 1.235.458,90 1,234,324 m (Where in this case m is to be conveyed as...
3
by: Caesar Augustus | last post by:
Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the...
2
by: Rolan | last post by:
I would appreciate anyone's assistance to sort out what change in code is needed for 'TextToDigits()' as shown below. The function does convert the text to numbers, but it is two decimals off,...
11
by: Penfold | last post by:
I'd appreciate help converting student average test scores into grades. My problem is that I need to allocate one of about 20 grades (3a,3b,3c,4a,4b,4c etc through to 8c plus a couple of others)....
1
by: pebelund | last post by:
Hi I got an .aspx that got the following code (example) <td width="120"> <% = row %> </td> row takes a value from a SQL db, that is a string of numbers. On the webbsite I want this to show...
4
by: chengsi | last post by:
I have based a report on a crosstab query. This query has eliminated all formats, i.e. all fields including those containing numbers are now stored as text. Is there a way that i can get Access...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
1
missinglinq
by: missinglinq | last post by:
From the Access Object Dialog Box click on "Modules" Click on new to make a new module Copy then paste the following code into the new module: Function SayNo(ByVal N As Currency) As String ...
0
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
clintonb wrote: There is nothing like an original double amount. If you care about rounding errors you must not store any currency value in an approximate number. You must not do this even once....
10
by: Guillermo_Lopez | last post by:
Hello All, I am using VBA in access to perform some calculations. There is a particular sumation that is wrong (barely). this code is withing a loop. TDist = TDist + TempDist Both TDist...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.