converting figures to words

puthenveetil

Registered User.
Local time
Today, 16:44
Joined
Aug 9, 2004
Messages
94
Dear all,

I am creating a database which has an invoice printing form. In that I would like to have a column for the total amount in words. I have got a sample module from northwind database. According to that 100,000 is "one hundred thousand" but in my country that is pronounced as " 1 Lakh" and for one million it is 10 lakh, for 10 million it is 1 crore like that. Is that possible to change the code in that module to display the words according to our standards? I am attaching the code with this thread. If anybody can show how to do that...I will be thankful to them..


Thanks


Function ConvertCurrencyToEnglish(ByVal MyNumber)
Dim Temp
Dim Dollars, Cents
Dim DecimalPlace, count

ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "

' Convert MyNumber to a string, trimming extra spaces.
MyNumber = Trim(Str(MyNumber))

' Find decimal place.
DecimalPlace = InStr(MyNumber, ".")

' If we find decimal place...
If DecimalPlace > 0 Then
' Convert cents
Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)
Cents = ConvertTens(Temp)

' Strip off cents from remainder to convert.
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If

count = 1
Do While MyNumber <> ""
' Convert last 3 digits of MyNumber to English dollars.
Temp = ConvertHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(count) & Dollars
If Len(MyNumber) > 3 Then
' Remove last 3 converted digits from MyNumber.
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
count = count + 1
Loop

' Clean up dollars.
Select Case Dollars
Case ""
Dollars = "No Dollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select

' Clean up cents.
Select Case Cents
Case ""
Cents = " And No Cents"
Case "One"
Cents = " And One Cent"
Case Else
Cents = " And " & Cents & " Cents"
End Select

ConvertCurrencyToEnglish = Dollars & Cents
End Function


Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
Case 1: ConvertDigit = "One"
Case 2: ConvertDigit = "Two"
Case 3: ConvertDigit = "Three"
Case 4: ConvertDigit = "Four"
Case 5: ConvertDigit = "Five"
Case 6: ConvertDigit = "Six"
Case 7: ConvertDigit = "Seven"
Case 8: ConvertDigit = "Eight"
Case 9: ConvertDigit = "Nine"
Case Else: ConvertDigit = ""
End Select

End Function

Private Function ConvertHundreds(ByVal MyNumber)
Dim result As String

' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then Exit Function

' Append leading zeros to number.
MyNumber = Right("000" & MyNumber, 3)

' Do we have a hundreds place digit to convert?
If Left(MyNumber, 1) <> "0" Then
result = ConvertDigit(Left(MyNumber, 1)) & " Hundred "
End If

' Do we have a tens place digit to convert?
If Mid(MyNumber, 2, 1) <> "0" Then
result = result & ConvertTens(Mid(MyNumber, 2))
Else
' If not, then convert the ones place digit.
result = result & ConvertDigit(Mid(MyNumber, 3))
End If

ConvertHundreds = Trim(result)
End Function


Private Function ConvertTens(ByVal MyTens)
Dim result As String

' Is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
Select Case Val(MyTens)
Case 10: result = "Ten"
Case 11: result = "Eleven"
Case 12: result = "Twelve"
Case 13: result = "Thirteen"
Case 14: result = "Fourteen"
Case 15: result = "Fifteen"
Case 16: result = "Sixteen"
Case 17: result = "Seventeen"
Case 18: result = "Eighteen"
Case 19: result = "Nineteen"
Case Else
End Select
Else
' .. otherwise it's between 20 and 99.
Select Case Val(Left(MyTens, 1))
Case 2: result = "Twenty "
Case 3: result = "Thirty "
Case 4: result = "Forty "
Case 5: result = "Fifty "
Case 6: result = "Sixty "
Case 7: result = "Seventy "
Case 8: result = "Eighty "
Case 9: result = "Ninety "
Case Else
End Select

' Convert ones place digit.
result = result & ConvertDigit(Right(MyTens, 1))
End If

ConvertTens = result
End Function
 
I dont understand what is your real problem. Is it you are unable to rewrite that function as per your need? Clear aayittu parayu koottukaara. I can help you by giving you such a function. Also let me know which will be your greatest number to be converted into words and the name of your currency. Is it Rs ?
 
Last edited:
Dont know how to do this!!!

Hello I have been trying to do this but to be honest I dont understad how to do it. I have copy and paste the code into a module. Checked it buy doing a Debug and nothing seems to be wrong.
Now on a report or form what do I need to put in the control Source? =MyNumber([field]) or NumWord([field]) I dont know what I need to do.

Sorry,

DaniBoy
 
thank you bob,

I have the code but i still dont understand how to make my field use this!! what do i put on the field?

thanks
 
If you are using the above code, try something like:

Me.ProcDisplay.ControlSource = ConvertCurrencyToEnglish([myField])
 
Where do I put that code!!

I get the codes but you I dont know where to put it? Form load, in what event ?
 
I figured it out!!!!

Ok, for you guys to make it easy for you, Copy the code above and paste on a new module, save module, then put an unbound field on the form or report where you want the numbers to word to be done.

just put this on the control source of the field

ConvertCurrencyToEnglish([myField])

on myfield put the other field that has the numbers...

This is how I did it, at least now I can play with it and see if I can put into a bound field.

See ya,

DaniBoy
 
thank you bob,

I have the code but i still dont understand how to make my field use this!! what do i put on the field?

thanks

As it's a public function it really doesn't matter where you place the code, public means that the project can find it regardless of where it is.

My personal preference though is to save all public functions in specified modules and give the modules a name that means something to me- BasPubApp (Basic code, Public functions, for this Application).
 

Users who are viewing this thread

Back
Top Bottom