Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 1st, 2004, 04:51 AM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

Reply With Quote
  #2 (permalink)  
Old October 1st, 2004, 05:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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...
Reply With Quote
  #3 (permalink)  
Old October 1st, 2004, 10:04 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Reply With Quote
  #4 (permalink)  
Old September 28th, 2007, 02:38 AM
Registered User
 
Join Date: Aug 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks

Reply With Quote
  #5 (permalink)  
Old October 1st, 2007, 07:13 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<%
    '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)
%>


Reply With Quote
  #6 (permalink)  
Old October 1st, 2007, 07:15 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the modified one of the previous post. Hope this
Could help you.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can XSLT convert amount to words LeoMathew XSLT 4 January 10th, 2018 04:09 AM
Convert Currency To Words Kenny Alligood Access 3 October 12th, 2017 06:00 AM
Numbers to words and vice versa? code_lover SQL Language 0 January 7th, 2007 04:50 PM
converting numbers to words ali_md SQL Server 2000 4 March 3rd, 2006 02:10 AM
convert number into words on acces form superparim Wrox Book Feedback 0 September 19th, 2005 01:55 PM



All times are GMT -4. The time now is 06:57 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.