convert dollars and cents to words

lala

Registered User.
Local time
Today, 12:13
Joined
Mar 20, 2002
Messages
741
i need a code to convert a check amount to the word amount to put on the check

i have a little form that prints checks (no, i can't use quicken and other programs like that, long story why i cant)
and i need to be able to convert the number to the word amount
 
brian
i tried your code
it works fine, but ig gives me an error when i open my form
but then it works anyway

what i did was i created a module, and then in my form made a textbox =spellnumber([amount])
amount is the box that has the number that i need converted

now every time i open my form it gives me the option to end or debug the error, and the error is INVALID USE OF NULL

when i click DEBUG, this is the line that's highlighted
MyNumber = Trim(Str(MyNumber))

and then it works anyway, as i said before


i will spend the time to figure it out if you can't help, but if you can, that'd be nice
its your code, so i figured, you know it inside and out
 
also, if you can help, there's a few more things
i want to leave cents as a number, not a word

and i want the word AND to appear between thousands and hundreds if that's all there is
or between hundreds and tens, if there's no thousands and no cents
or between thousands and tens, if there's no hundreds and no cents

do you get it?

i have it working now, except for that it doesn't convert my number, i havd to break it up in thousands, hundreds, dollars and cents
and then i have a box that says this

=IIf(Not IsNull([Thou]),[ThouW] & " Thousand ","") & IIf(Not IsNull([thou]) And Not IsNull([hun]) And IsNull([dollars]) And IsNull([cents]),"And ","") & IIf(Not IsNull([thou]) And IsNull([hun]) And Not IsNull([dollars]) And IsNull([cents]),"And ","") & IIf(Not IsNull([hun]),[hunw] & " Hundred ","") & IIf(IsNull([thou]) And Not IsNull([hun]) And Not IsNull([dollars]) And IsNull([cents]),"And ","") & IIf(Not IsNull([dollars]),[dollarsw] & " Dollars ","") & IIf(IsNull([dollars]) And (Not IsNull([thou]) Or Not IsNull([hun])),"Dollars ","") & IIf(Not IsNull([cents]),"And " & [cents] & " Cents","")


and it works just as i want it


sorry to take too much of your time
it's not that serious
so if you feel like it - then help me out, if not - it's not that important
 
Hi
Firstly it is not my code I just helped the guy use it. :)

To overcome the Null problem the simplest is to put

If IsNull(MyNumber) Then MyNumber = 0

before the Trim statement, but you may prefer to create code saying "Null value" and exit the Function.

As to the rest I haven't looked at that, it could mean extensive changes and it might be simple.

Brian
 
Hi again took a look after my evening meal and keeping Cents as a number is not to difficult

2 areas of change

Code:
' String representation of amount
If IsNull(MyNumber) Then MyNumber = 0
MyNumber = Trim(Str(MyNumber))

' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Keep Numeric cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = Left(Mid(MyNumber, DecimalPlace + 1), Len(MyNumber - DecimalPlace))
If (Len(MyNumber) - DecimalPlace) = 1 Then
Cents = Cents * 10
End If
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If

Code:
Select Case Cents
Case ""
Cents = " and No Cents"
Case 1
Cents = " and " & Cents & " Cent"
Case Else
Cents = " and " & Cents & " Cents"
End Select

But I think that I may have to leave you with your IIfs for the rest. Sorry

Brian
 
guys, thank you so much


it's working fine, but i'm a perfectionist

is there a way to have it put AND between hundreds and tens, if there's no cents
or between thousands and hundreds, if there's no cents and no tens
or between thousands and tens, if there's no cents and no hundreds
 
I don't think I follow your requirements , don't we normally just put an and before the tens/units in all cases. the modified code below produces the following.

Ten Dollars and 10 Cents
One Hundred and Ten Dollars and 10 Cents
One Hundred Dollars and 10 Cents

One Hundred and Twenty Three Billion Four Hundred and Fifty Six Million Seven Hundred and Eighty Nine Thousand One Hundred and Twenty Three Dollars and No Cents

Anymore you can test yourself.:)
Hope it gives you what you want.

Brian

Code:
Public Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count

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

' String representation of amount
If IsNull(MyNumber) Then MyNumber = 0
MyNumber = Trim(Str(MyNumber))

' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Keep Numeric cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = Left(Mid(MyNumber, DecimalPlace + 1), Len(MyNumber - DecimalPlace))
If (Len(MyNumber) - DecimalPlace) = 1 Then
Cents = Cents * 10
End If
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If

Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop

Select Case Dollars
Case ""
Dollars = "No Dollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select

Select Case Cents
Case ""
Cents = " and No Cents"
Case 1
Cents = " and " & Cents & " Cent"
Case Else
Cents = " and " & Cents & " Cents"
End Select

SpellNumber = Dollars & Cents
If Left(SpellNumber, 3) = "and" Then    'No hundreds etc
SpellNumber = Mid(SpellNumber, 5)       'remove and
End If
End Function

'*******************************************
' Converts a number from 100-999 into text *
'*******************************************

Function GetHundreds(ByVal MyNumber)
Dim Result As String

If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)

' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If

' Convert the tens and ones place.
If Mid(MyNumber, 2, 2) = 0 Then
    GetHundreds = Result
    Exit Function                           ' no tens and units
    Else
    Result = Result & "and "                ' place "and" before tens and ones
    End If

If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If

GetHundreds = Result
End Function

'*********************************************
' Converts a number from 10 to 99 into text. *
'*********************************************

Function GetTens(TensText)
Dim Result As String

Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
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 ' If value between 20-99...
Select Case Val(Left(TensText, 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
Result = Result & GetDigit _
(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function


'*******************************************
' Converts a number from 1 to 9 into text. *
'*******************************************

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

i only want 1 AND, and it goes between the last 2 numbers, whatever they are

three thousand two hundred and 35 cents
three hundred and three dollars
four thousand and 45 cents
four thousand and four dollars
three thousand five hundred and twenty five dollars


you get it?
 
you don't have to work on it
i feel bad making you spend time on it
 
I look on it as a challenge. :D
But I think that you may have beaten me on this one so don't hold your breath. :)

Brian
 
Although I have done some testing this comes without any garauntees :D , so please test.
You need to replace the Function spellnumber and add the function finaledit.
All other functions remain the same.

Brian

Public Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count

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

' String representation of amount
If IsNull(MyNumber) Then MyNumber = 0
MyNumber = Trim(Str(MyNumber))

' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Keep Numeric cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = Left(Mid(MyNumber, DecimalPlace + 1), Len(MyNumber - DecimalPlace))
If (Len(MyNumber) - DecimalPlace) = 1 Then
Cents = Cents * 10
End If
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If

Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop

Select Case Dollars
Case ""
Dollars = "No Dollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select

Select Case Cents
Case ""
Cents = ""
Case 1
Cents = " and " & Cents & " Cent"
Case Else
Cents = " and " & Cents & " Cents"
End Select

textnumber = Dollars & Cents
SpellNumber = finaledit(textnumber)
End Function


Function finaledit(ByVal textnumber)

posdoll = InStr(1, textnumber, "Dollars")
poshun = InStrRev(textnumber, "hundred")
posthou = InStrRev(textnumber, "thousand")
If InStr(1, textnumber, "cent") <> 0 Then 'if cents then and present
finaledit = textnumber
Exit Function
Else
If poshun = 0 Then GoTo nohundreds
If posdoll - (poshun + 7) > 2 Then 'tens and/or units present
finaledit = Left(textnumber, (poshun + 7)) & "and " & Mid(textnumber, (poshun + 8))
Exit Function
ElseIf posthou = 0 Then
finaledit = textnumber 'only hundred present
Exit Function
Else
'no tens and units place and after thousand
finaledit = Left(textnumber, (posthou + 8)) & "and " & Mid(textnumber, (posthou + 9))
Exit Function
End If
nohundreds:
If posdoll - (posthou + 8) > 2 Then 'tens and/or units present
finaledit = Left(textnumber, (posthou + 8)) & "and " & Mid(textnumber, (posthou + 9))
Exit Function
Else
'no tens and units
finaledit = textnumber
Exit Function
End If
End If
finaledit = textnumber 'catch all
End Function
 

Users who are viewing this thread

Back
Top Bottom