 |
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

October 1st, 2004, 04:51 AM
|
Registered User
|
|
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

October 1st, 2004, 05:09 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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...
|

October 1st, 2004, 10:04 AM
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|

September 28th, 2007, 02:38 AM
|
Registered User
|
|
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks
|

October 1st, 2007, 07:13 AM
|
Registered User
|
|
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
<%
'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)
%>
|

October 1st, 2007, 07:15 AM
|
Registered User
|
|
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This is the modified one of the previous post. Hope this
Could help you.
|
|
 |