DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Please help me out

1. Senior Member
Join Date
Aug 2004
Posts
43,023

## Please help me out

[Originally posted by Roopa]

Is it possible to convert number into number names. For ex: \$450 to Dollar Four Hundred and Fifty.
If so then please can anyone provide me the code for the same.
It will be very helpful if anyone helps me out with this.
reply me to benzer@eth.net
thanks in advance

2. Senior Member
Join Date
Aug 2004
Posts
43,023

## Re:Please help me out

[Originally posted by Coyote]

Hi,
This should help. I didn't write this and do not know the author to pass the credit to, but here is a code snippet that I collected that works:

'*****Code Starts ******
Function ConvertNumberToEnglish(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 = ""
ÿ ÿ ÿ ÿ ÿ ÿ Case "One"

ÿ ÿ ÿ ÿ ÿ ÿ ÿ Dollars = ""
ÿ ÿ ÿ ÿ ÿ ÿ Case Else
ÿ ÿ ÿ ÿ ÿ ÿ ÿ Dollars = Dollars & ""
ÿ ÿ ÿ ÿ End Select

ÿ ÿ ÿ ÿ ' Clean up cents.
ÿ ÿ ÿ ÿ Select Case Cents
ÿ ÿ ÿ ÿ ÿ ÿ Case ""
ÿ ÿ ÿ ÿ ÿ ÿ ÿ Cents = ""
ÿ ÿ ÿ ÿ ÿ ÿ Case "One"
ÿ ÿ ÿ ÿ ÿ ÿ ÿ Cents = ""
ÿ ÿ ÿ ÿ ÿ ÿ Case Else
ÿ ÿ ÿ ÿ ÿ ÿ ÿ Cents = "" & Cents & ""
ÿ ÿ ÿ ÿ End Select

ÿ ÿ ÿ ÿ ConvertNumberToEnglish = 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
' **** Code Ends **********

If you have a problem - give a howl..
>>>Coyote<<<

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source