p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Classic ASP Basics (http://p2p.wrox.com/forumdisplay.php?f=61)
-   -   Convert Numbers to Words (http://p2p.wrox.com/showthread.php?t=19243)

 hasankelepir October 1st, 2004 04:51 AM

Convert Numbers to Words

Hi. I am trying to get a number from a text field and in the next page I want to write both this number as digits and as words.
For example: \$147,25 - onehundredfortyseven dollars and twentyfive cents. I found a code in this site relevant for this but it didn't work.
I would appreciate it if anyone could help me to do this.

 pgtips October 1st, 2004 05:09 AM

why don't you post the code you have already, and tell us what errors you get, or define "it didnt work" and we'll see what we can do...

 ChrisScott October 1st, 2004 10:04 AM

I know this could be seriously optimised & has an upper limit, but it should get you started...

Code:

```Response.Write ExpandPrice("\$347592634554147,25") & "<br />" Response.Write ExpandPrice("\$147,25") Function ExpandPrice(pPrice)     Dim temp: temp = ""     Dim expr: Set expr = New RegExp     expr.Pattern = "^\\$(\d+),(\d\d)\$"     If expr.test(pPrice) Then         Dim dollars: dollars = expr.Replace(pPrice, "\$1")         Dim cents: cents = expr.Replace(pPrice, "\$2")         If CDbl(dollars) > 0 Then             temp = temp & ExpandNumber(dollars) & " dollars"             If CDbl(cents) > 0 Then                 temp = temp & " and "             End If         End If         If CDbl(cents) > 0 Then             temp = temp & ExpandNumber(cents) & " cents"         End If     End If     Set expr = Nothing     ExpandPrice = temp End Function Function ExpandNumber(pNumberStr)     Dim temp: temp = ""     Dim suffixes: suffixes = Array("thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion") ' U.S. '    Dim suffixes: suffixes = Array("thousand", "million", "milliard", "billion", "billiard", "trillion", "trilliard") ' European     Dim number: number = String(3 - Len(pNumberStr) Mod 3, "0") & pNumberStr     Dim i, j: j = -1     For i = Len(number) - 2 To 1 Step -3         numPart = Mid(number, i, 3)         If Clng(numPart > 0) Then             If j > -1 Then                 temp = suffixes(j) & temp             End If             temp = GetNumberUnder1000Str(numPart) & temp         End If            j = j + 1     Next     ExpandNumber = temp End Function Function GetNumberUnder1000Str(pNumber)     Dim temp: temp = ""     If Len(pNumber) = 3 Then         If CLng(Left(pNumber, 1)) > 0 Then             temp = temp & GetNumberUnder100Str(Left(pNumber, 1)) & "hundred"         End If        End If     temp = temp & GetNumberUnder100Str(Right("0" & pNumber, 2))     GetNumberUnder1000Str = temp End Function Function GetNumberUnder100Str(pNumber)     Dim units: units = Array("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", _                         "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")     If pNumber > 19 Then         Dim tens: tens = Array("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")         GetNumberUnder100Str = tens(Left(pNumber, 1) - 2) & units(Right(pNumber, 1))     Else         GetNumberUnder100Str = units(Right(pNumber, 1))     End If End Function```
HTH,

Chris

 star12345 September 28th, 2007 02:38 AM

Thanks

 Tvarun October 1st, 2007 07:13 AM

<%
'Max Number = \$999999999999999999999999,99' 24 Digit Number(for Dollar Palce) and 2 Digit number for Cent Place
Function ExpandPrice(pPrice)
Dim temp: temp = ""
Dim expr: Set expr = New RegExp
expr.Pattern = "^\\$(\d+),(\d\d)\$"
If expr.test(pPrice) Then
Dim dollars: dollars = expr.Replace(pPrice, "\$1")
Dim cents: cents = expr.Replace(pPrice, "\$2")
'Response.Write(cents)
If CDbl(dollars) > 1 Then
temp = temp & ExpandNumber(dollars) & " Dollars"
If CDbl(cents) > 0 Then
temp = temp & " And "
End If
ElseIf CDbl(dollars) = 0 Then
temp = temp & ExpandNumber(dollars) & " Zero Dollars "
If CDbl(cents) >= 0 Then
temp = temp & " And "
End If
ElseIf CDbl(dollars) = 1 Then
temp = temp & ExpandNumber(dollars) & " Dollar "
End If

If CDbl(cents) > 1 Then
temp = temp & ExpandNumber(cents) & " Cents"
ElseIf CDbl(cents) = 0 Then
temp = temp & ExpandNumber(cents) & " Zero Cents "
ElseIf CDbl(cents) = 1 Then
temp = temp & ExpandNumber(cents) & " Cent "
End If
End If
Set expr = Nothing
ExpandPrice = temp
End Function

Function ExpandNumber(pNumberStr)
Dim temp: temp = ""
Dim suffixes: suffixes = Array("Thousand ", "Million ", "Billion ", "Trillion ", "Quadrillion ", "Quintillion ", "Sextillion ") ' U.S.
'Dim suffixes: suffixes = Array("Thousand ", "Million ", "Milliard ", "Billion ", "Billiard ", "Trillion ", "Trilliard ") ' European
Dim number: number = String(3 - Len(pNumberStr) Mod 3, "0") & pNumberStr
Dim i, j: j = -1
Dim numPart
For i = Len(number) - 2 To 1 Step -3
numPart = Mid(number, i, 3)
If Clng(numPart > 0) Then
If j > -1 Then
temp = suffixes(j) & temp
End If
temp = GetNumberUnder1000Str(numPart) & temp
End If
j = j + 1
Next
ExpandNumber = temp
End Function

Function GetNumberUnder1000Str(pNumber)
Dim temp: temp = ""
If Len(pNumber) = 3 Then
If CLng(Left(pNumber, 1)) > 0 Then
temp = temp & GetNumberUnder100Str(Left(pNumber, 1)) & " Hundred "
End If
End If
temp = temp & GetNumberUnder100Str(Right("0" & pNumber, 2))
GetNumberUnder1000Str = temp
End Function

Function GetNumberUnder100Str(pNumber)
Dim units: units = Array("", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ")
Dim tens: tens = Array("Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety ")
Dim Digits: Digits = Array("Ten ","Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen")

If pNumber > 19 Then
GetNumberUnder100Str = tens(Left(pNumber, 1) - 2) & units(Right(pNumber, 1))
ElseIF pNumber >= 10 and pNumber <= 19 Then
GetNumberUnder100Str = Digits(Right(pNumber, 1))
Else
GetNumberUnder100Str = units(Right(pNumber, 1))
End If
End Function

'Example : Response.Write ExpandPrice("\$1,99") & "<br />"
Dim vDollar,vCent,vAmount
vDollar = 21121221
vCent = 99
vAmount = "\$"&vDollar&","&vCent
Response.Write vAmount &"<br>"
Response.Write ExpandPrice(vAmount)
%>

 Tvarun October 1st, 2007 07:15 AM

This is the modified one of the previous post. Hope this