Question

Convert Number into text automatically

Asked by: CHiLiNVLn

On Access 2000, I have a program designed to print off paychecks for employees on an automatic payroll system.  The system only provides me with there name, date, and pay in dollar amount in a digit field.  Is there a way to interpret the digit and fill in the text automatically.
Example.  $2,500.00 would write out Two thousand five hundred dollars and ****00 on the check.  Thanks?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2000-01-27 at 12:09:51ID10266584
Tags

convert

,

number

,

access

,

text

Topic

Microsoft Access Database

Participating Experts
6
Points
150
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Convert a dollar value to a text
    A long time ago I stumbled across the code to convert a dollar value to a text format. For example the code would convert 11,382 to "Eleven thousand three hundred eighty-two" A timely answer would be greatly appreciated, especially since this question has been ans...
  2. converting dollar amount to dollar description
    converting dollar amount to dollar description is there a VB function that can take a dollar amount say $123.50 and translate it into a description of: one hundred and twenty three dollars and fifty cents or one hundred and twenty three dollars and 50/100
  3. Convert Dollars Amount Into Words
    Hello EE, I am being presenting with a simple, yet challenging issue and would like to ask all EE to see if anyone know of a SQL function or if anyone know of a good way handle this. myTable Rec Col1 Col2 1 10911.95 2 3181.00 3 506....
  4. Converting of digits in dollars in numbers to words
    Hi, I would like to know whats the most efficient way to convert digits to words i.e 12.00 to twelve dollars 12.34 to twelve dollars and thirty-four cents. Thanks.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ssteevesPosted on 2000-01-27 at 12:23:15ID: 2394267

Here's a function I wrote for fun one day.  Call it like this:

msgbox(WrittenAmount(3400))

It's pretty big, but here it is:

Function WrittenAmount(x As Currency)
'Takes a numeric amount and translates it to words
Dim Temp As String
Dim Results As String
Dim Negative As Boolean
Dim z As String 'Used in Recursive Call
Dim varDecimal
Dim y

Temp = Trim(Str(x))
Temp = Int(Temp)

varDecimal = Format(x - Int(x), "#.00")

'Check for Negative
If Left$(Temp, 1) = "-" Then
  Negative = True
  Temp = Mid$(Temp, 2, Len(Temp))
End If

If Len(Temp) = 1 Then GoTo Ones
If Len(Temp) = 2 Then GoTo Tens
If Len(Temp) = 3 Then GoTo Hundreds
If Len(Temp) = 4 Then GoTo Thousands
If Len(Temp) = 5 Then GoTo TenThousands
If Len(Temp) = 6 Then GoTo HundredThousands
If Len(Temp) = 7 Then GoTo Millions

Millions:
'Evaluate Millions Place
 Select Case Mid$(Temp, Len(Temp) - 6, 1)
   Case 1
     Results = Results & "One Million "
   Case 2
     Results = Results & "Two Million "
   Case 3
     Results = Results & "Three Million "
   Case 4
     Results = Results & "Four Million "
   Case 5
     Results = Results & "Five Million "
   Case 6
     Results = Results & "Six Million "
   Case 7
     Results = Results & "Seven Million "
   Case 8
     Results = Results & "Eight Million "
   Case 9
     Results = Results & "Nine Million "
   End Select


HundredThousands:
'Evaluate Hundred Thousands Place
y = Right$(Temp, 6)
z = WrittenAmount(Left$(y, 3))
If Right$(z, 7) = "Dollars" Then
  z = Trim(Mid$(WrittenAmount(Left$(y, 3)), 1, Len(WrittenAmount(Left$(y, 3))) - 7))
End If
If z <> "" Then
  Results = Results & z & " Thousand and "
End If
GoTo Hundreds


TenThousands:
'Evaluate Ten Thousands Place
y = Right$(Temp, 6)
z = WrittenAmount(Left$(y, 2))
If Right$(z, 7) = "Dollars" Then
  z = Trim(Mid$(WrittenAmount(Left$(y, 2)), 1, Len(WrittenAmount(Left$(y, 2))) - 7))
End If
Results = Results & z & " Thousand and "
GoTo Hundreds


Thousands:
'Evaluate Thousands Place
 Select Case Mid$(Temp, Len(Temp) - 3, 1)
   Case 1
     Results = Results & "One Thousand "
   Case 2
     Results = Results & "Two Thousand "
   Case 3
     Results = Results & "Three Thousand "
   Case 4
     Results = Results & "Four Thousand "
   Case 5
     Results = Results & "Five Thousand "
   Case 6
     Results = Results & "Six Thousand "
   Case 7
     Results = Results & "Seven Thousand "
   Case 8
     Results = Results & "Eight Thousand "
   Case 9
     Results = Results & "Nine Thousand "
   End Select
   
   
Hundreds:
'Evaluate Hundreds Place
 If (Right$(Results, 4) = "and ") And Mid$(Temp, Len(Temp) - 2, 1) <> "0" Then
   Results = Mid$(Results, 1, Len(Results) - 4)
 End If
 If Right$(Results, 5) = "Thous" Then
   Results = Results & "and "
 End If
 Select Case Mid$(Temp, Len(Temp) - 2, 1)
   Case 1
     Results = Results & "One Hundred and "
   Case 2
     Results = Results & "Two Hundred and "
   Case 3
     Results = Results & "Three Hundred and "
   Case 4
     Results = Results & "Four Hundred and "
   Case 5
     Results = Results & "Five Hundred and "
   Case 6
     Results = Results & "Six Hundred and "
   Case 7
     Results = Results & "Seven Hundred and "
   Case 8
     Results = Results & "Eight Hundred and "
   Case 9
     Results = Results & "Nine Hundred and "
 End Select
 
 
Tens:
'Evaluate Ten's Place
 Select Case Mid$(Temp, Len(Temp) - 1, 1)
   Case 1
     If Right$(Temp, 2) = 10 Then
       Results = Results & "Ten ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 11 Then
       Results = Results & "Eleven ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 12 Then
       Results = Results & "Twelve ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 13 Then
       Results = Results & "Thirteen ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 14 Then
       Results = Results & "Fourteen ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 15 Then
       Results = Results & "Fifteen ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 16 Then
       Results = Results & "Sixteen ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 17 Then
       Results = Results & "Seventeen ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 18 Then
       Results = Results & "Eighteen ": GoTo TheEnd
     ElseIf Right$(Temp, 2) = 19 Then
       Results = Results & "Ninteen ": GoTo TheEnd
     End If
   Case 2
     Results = Results & "Twenty "
   Case 3
     Results = Results & "Thirty "
   Case 4
     Results = Results & "Fourty "
   Case 5
     Results = Results & "Fifty "
   Case 6
     Results = Results & "Sixty "
   Case 7
     Results = Results & "Seventy "
   Case 8
     Results = Results & "Eighty "
   Case 9
     Results = Results & "Ninty "
 End Select
 
Ones:
'Evaluate One's Place
  Select Case Right$(Temp, 1)
   Case 1
      Results = Results & "One "
   Case 2
      Results = Results & "Two "
   Case 3
      Results = Results & "Three "
   Case 4
      Results = Results & "Four "
   Case 5
      Results = Results & "Five "
   Case 6
      Results = Results & "Six "
   Case 7
      Results = Results & "Seven "
   Case 8
      Results = Results & "Eight "
   Case 9
      Results = Results & "Nine "
  End Select
GoTo TheEnd

TheEnd:

If Right$(Trim(Results), 4) = " and" Then
  Results = Trim(Mid$(Results, 1, Len(Results) - 4))
End If

If Negative = True Then
  Results = "Negative " & Results
End If
If Right$(Results, 1) <> " " Then
  Results = Results & " Dollars"
Else
  Results = Results & "Dollars"
End If

If Left$(varDecimal, 1) = "." Then
  varDecimal = Right$(varDecimal, 2)
  varDecimal = Val(varDecimal)
End If
Dim a
If varDecimal <> 0 Then
  a = WrittenAmount(Val(varDecimal))
  If Right$(a, 7) = "Dollars" Then
    a = Trim(Mid$(a, 1, Len(a) - 7))
  End If
End If
If a <> "" Then Results = Results & " and " & a & " Cents."
WrittenAmount = Results
End Function

 

by: ssteevesPosted on 2000-01-27 at 12:24:02ID: 2394272

Place that code in a Module.  It's probably the best place for it.

 

by: wesleystewartPosted on 2000-01-27 at 13:57:05ID: 2394604

Wow.  That's a monster.  And it works!  Give this man some points!!

Wes

 

by: JimMorganPosted on 2000-01-27 at 15:24:13ID: 2394906

It is innovative.  I have used a shorter procedure which I wrote back in the BASIC for DOS days.  I'll have to do some testing to see which procedure works the fastest.

The only question would be in today's big bucks environment, would there be a need for a billion dollar case?

Jim

 

by: bclarkPosted on 2000-01-27 at 16:15:55ID: 2395042

Also check
http://www.mvps.org/access/modules/mdl0001.htm

home page is
http://www.mvps.org/access/index.html

click on "Modules" from the list on the left
select "Convert Currency ($500) into words (Five Hundred Dollars)" from the list in upper right frame



---Posted by Joe Foster---

Convert Currency ($500) into words (Five Hundred Dollars)

(Q)    I'm looking to have Access convert the currency value "$531.20" into the text "Five hundred thirty one and twenty cents."

(A)    Use the following function provided by Joe Foster to convert the Currency into English words.

'************ Code Start **********
'This code was originally written by Joe Foster.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Joe Foster
'
' Convert a currency value into an (American) English string
Function English (ByVal N As Currency) As String
    Const Thousand = 1000@
    Const Million = Thousand * Thousand
    Const Billion = Thousand * Million
    Const Trillion = Thousand * Billion

    If (N = 0@) Then English = "zero": Exit Function

    Dim Buf As String: If (N < 0@) Then Buf = "negative " Else Buf = ""
    Dim Frac As Currency: Frac = Abs(N - Fix(N))
    If (N < 0@ Or Frac <> 0@) Then N = Abs(Fix(N))
    Dim AtLeastOne As Integer: AtLeastOne = N >= 1

    If (N >= Trillion) Then
        Debug.Print N
        Buf = Buf & EnglishDigitGroup(Int(N / Trillion)) & " trillion"
        N = N - Int(N / Trillion) * Trillion ' Mod overflows
        If (N >= 1@) Then Buf = Buf & " "
    End If
   
    If (N >= Billion) Then
        Debug.Print N
        Buf = Buf & EnglishDigitGroup(Int(N / Billion)) & " billion"
        N = N - Int(N / Billion) * Billion ' Mod still overflows
        If (N >= 1@) Then Buf = Buf & " "
    End If

    If (N >= Million) Then
        Debug.Print N
        Buf = Buf & EnglishDigitGroup(N \ Million) & " million"
        N = N Mod Million
        If (N >= 1@) Then Buf = Buf & " "
    End If

    If (N >= Thousand) Then
        Debug.Print N
        Buf = Buf & EnglishDigitGroup(N \ Thousand) & " thousand"
        N = N Mod Thousand
        If (N >= 1@) Then Buf = Buf & " "
    End If

    If (N >= 1@) Then
        Debug.Print N
        Buf = Buf & EnglishDigitGroup(N)
    End If

    If (Frac = 0@) Then
        Buf = Buf & " exactly"
    ElseIf (Int(Frac * 100@) = Frac * 100@) Then
        If AtLeastOne Then Buf = Buf & " and "
        Buf = Buf & Format$(Frac * 100@, "00") & "/100"
    Else
        If AtLeastOne Then Buf = Buf & " and "
        Buf = Buf & Format$(Frac * 10000@, "0000") & "/10000"
    End If

    English = Buf
End Function

' Support function to be used only by English()
Private Function EnglishDigitGroup (ByVal N As Integer) As String
    Const Hundred = " hundred"
    Const One = "one"
    Const Two = "two"
    Const Three = "three"
    Const Four = "four"
    Const Five = "five"
    Const Six = "six"
    Const Seven = "seven"
    Const Eight = "eight"
    Const Nine = "nine"
    Dim Buf As String: Buf = ""
    Dim Flag As Integer: Flag = False

    'Do hundreds
    Select Case (N \ 100)
    Case 0: Buf = "":  Flag = False
    Case 1: Buf = One & Hundred: Flag = True
    Case 2: Buf = Two & Hundred: Flag = True
    Case 3: Buf = Three & Hundred: Flag = True
    Case 4: Buf = Four & Hundred: Flag = True
    Case 5: Buf = Five & Hundred: Flag = True
    Case 6: Buf = Six & Hundred: Flag = True
    Case 7: Buf = Seven & Hundred: Flag = True
    Case 8: Buf = Eight & Hundred: Flag = True
    Case 9: Buf = Nine & Hundred: Flag = True
    End Select
   
    If (Flag <> False) Then N = N Mod 100
    If (N > 0) Then
        If (Flag <> False) Then Buf = Buf & " "
    Else
        EnglishDigitGroup = Buf
        Exit Function
    End If
     
    'Do tens (except teens)
    Select Case (N \ 10)
    Case 0, 1: Flag = False
    Case 2: Buf = Buf & "twenty": Flag = True
    Case 3: Buf = Buf & "thirty": Flag = True
    Case 4: Buf = Buf & "forty": Flag = True
    Case 5: Buf = Buf & "fifty": Flag = True
    Case 6: Buf = Buf & "sixty": Flag = True
    Case 7: Buf = Buf & "seventy": Flag = True
    Case 8: Buf = Buf & "eighty": Flag = True
    Case 9: Buf = Buf & "ninety": Flag = True
    End Select
   
    If (Flag <> False) Then N = N Mod 10
    If (N > 0) Then
        If (Flag <> False) Then Buf = Buf & "-"
    Else
        EnglishDigitGroup = Buf
        Exit Function
    End If
   
    'Do ones and teens
    Select Case (N)
    Case 0: ' do nothing
    Case 1: Buf = Buf & One
    Case 2: Buf = Buf & Two
    Case 3: Buf = Buf & Three
    Case 4: Buf = Buf & Four
    Case 5: Buf = Buf & Five
    Case 6: Buf = Buf & Six
    Case 7: Buf = Buf & Seven
    Case 8: Buf = Buf & Eight
    Case 9: Buf = Buf & Nine
    Case 10: Buf = Buf & "ten"
    Case 11: Buf = Buf & "eleven"
    Case 12: Buf = Buf & "twelve"
    Case 13: Buf = Buf & "thirteen"
    Case 14: Buf = Buf & "fourteen"
    Case 15: Buf = Buf & "fifteen"
    Case 16: Buf = Buf & "sixteen"
    Case 17: Buf = Buf & "seventeen"
    Case 18: Buf = Buf & "eighteen"
    Case 19: Buf = Buf & "nineteen"
    End Select

    EnglishDigitGroup = Buf
End Function
'************ Code End **********


 

 

by: DedushkaPosted on 2000-01-27 at 20:45:15ID: 2395511

Hi, CHiLiNVLn.

More than year ago I answered this question and now I found this answer in my archiv
You just need to change FILS to CENTS and KUWAITI DINAR to USD:

-----------------------------------------------------------
The translation of moneys in words from my point of view requires no more than 50 lines of a code (VBA MSA97).

----------
Function say(curAMOUNT As Currency) As String
Dim intTmp As Integer, strAmount As String, strKop As String

strAmount = Format$(curAMOUNT, "#0.000")
strKop = Right(strAmount, 3)
If strKop = "000" Then
strKop = "only"
Else
strKop = "and FILS " & strKop & " only"
End If

strAmount = Left(strAmount, Len(strAmount) - 4)
If strAmount = "0" Then
    say = "NO KUWAITI DINAR "
Else
    say = "KUWAITI DINAR "
    Select Case Len(strAmount) Mod 3
        Case 1:    strAmount = "00" & strAmount
        Case 2:    strAmount = "0" & strAmount
    End Select
    For intTmp = Len(strAmount) \ 3 To 1 Step -1
        say = say & say_triada(Mid(strAmount, Len(strAmount) - 3 * intTmp + 1, 3), intTmp)
    Next
End If
say = say & strKop
End Function

Function say_triada(ByVal triada As String, ByVal triada_no As Integer) As String
Dim intTemp As Integer, strTemp  As String

If triada = "000" And triada_no > 1 Then Exit Function

say_triada = Choose(Left(triada, 1) + 1, "", "One ", "Two ", "Three ", "Four ", _
           "Five ", "Six ", "Seven ", "Eight ", "Nine ")
If say_triada <> "" Then say_triada = say_triada & "Houndred "

If Right(triada, 2) > 19 Then
    intTemp = Right(triada, 1)
    say_triada = say_triada & _
    Choose(Mid(triada, 2, 1) + 1, "", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", _
                                "Sixty ", "Seventy ", "Eighty ", "Ninety ")
Else
    intTemp = Right(triada, 2)
End If
     
say_triada = say_triada & Choose(intTemp + 1, _
            "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", _
            "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", _
            "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", _
            "Seventeen ", "Eighteen ", "Nineteen ") _
            & Choose(triada_no, "", "Thousand ", "Million ", "Billion ", "Trillion ")
End Function
---------
Cheers!
Dedushka

 

by: DedushkaPosted on 2000-01-27 at 20:49:37ID: 2395524

For USD amount first 13 lines must be:

strAmount = Format$(curAMOUNT, "#0.00")
strKop = Right(strAmount, 3)
If strKop = "00" Then
strKop = "only"
Else
strKop = "and CENTS " & strKop & " only"
End If

strAmount = Left(strAmount, Len(strAmount) - 3)
If strAmount = "0" Then
    say = "NO USD "
Else
    say = "USD "

 

by: DedushkaPosted on 2000-01-27 at 20:49:47ID: 2395527

For USD amount first 13 lines must be:

strAmount = Format$(curAMOUNT, "#0.00")
strKop = Right(strAmount, 2)
If strKop = "00" Then
strKop = "only"
Else
strKop = "and CENTS " & strKop & " only"
End If

strAmount = Left(strAmount, Len(strAmount) - 3)
If strAmount = "0" Then
    say = "NO USD "
Else
    say = "USD "

 

by: TrygvePosted on 2000-01-27 at 23:50:46ID: 2395751

Would someone like to benchmark these solutions to find the most effective one?

 

by: DedushkaPosted on 2000-01-28 at 00:42:07ID: 2395843

Trigve, I have a new verson of this routine, but it is for Russian rubles so is more complex than for USD. Later I'll rewrite it for USD and it will be more effective than that I post here:-)

 

by: DedushkaPosted on 2000-01-28 at 01:24:50ID: 2395940

Following code is 40 lines only:-)

Public Function Say(curAmount As Currency) As String
Dim intTmp As Integer, strAmount As String, strCent As String

strAmount = Format(curAmount, "#0.00")
strCent = " and " & Right(strAmount, 2) & " cents"
strAmount = Left(strAmount, Len(strAmount) - 3)
If strAmount = "0" Then Say = "No dollars" & strCent: Exit Function

strAmount = Choose((Len(strAmount) Mod 3), "00", "0") & strAmount
For intTmp = Len(strAmount) \ 3 To 1 Step -1
    Say = Say & SayTriada(Mid(strAmount, Len(strAmount) - 3 * intTmp + 1, 3), intTmp)
Next
Say = UCase(Left(Say, 1)) & Right(Say, Len(Say) - 1) & "dollars" & strCent

End Function

Public Function SayTriada(ByVal Triada As String, ByVal TriadaNo As Integer) As String
Dim intTemp As Integer, strTemp As String

If Triada = "000" And TriadaNo > 1 Then Exit Function

SayTriada = Choose(Left(Triada, 1) + 1, "", "One ", "Two ", "Three ", "Four ", _
           "Five ", "Six ", "Seven ", "Eight ", "Nine ")
If SayTriada <> "" Then SayTriada = SayTriada & "Houndred "
If Right(Triada, 2) > 19 Then
    intTemp = Right(Triada, 1)
    SayTriada = SayTriada & _
    Choose(Mid(Triada, 2, 1) + 1, "", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", _
                                "Sixty ", "Seventy ", "Eighty ", "Ninety ")
Else
    intTemp = Right(Triada, 2)
End If    
SayTriada = SayTriada & Choose(intTemp + 1, _
            "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", _
            "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", _
            "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", _
            "Seventeen ", "Eighteen ", "Nineteen ") _
            & Choose(TriadaNo, "", "Thousand ", "Million ", "Billion ", "Trillion ")
End Function

Cheers,
Dedushka

 

by: JimMorganPosted on 2000-01-28 at 02:34:54ID: 2396120

Dedushka:  I learned something with your suggestions.  I wasn't aware of the Choose function before.  I don't know how I missed that one.  I see its been around since VB4 but it isn't something that I was every introduced to.  Makes for some tight code.

Thanks for the info.

I threatened this once before and I guess I'll have to dig out the code tomorrow.  I remember writing a conversion routine some years ago which I seem to remember was only about 20 lines long and ran like a speed demon.

Jim

 

by: wesleystewartPosted on 2000-01-28 at 04:28:17ID: 2396396

On my machine, Dedushka's Say() function is about twice as fast, taking an average of 400ms to convert all numbers from 100000 to 101111.  ssteeve's function averaged just over 700ms.

I think it could be faster if you converted the number to a byte array rather than a string.  I'll have to work on it . . .

Wes

 

by: wesleystewartPosted on 2000-01-28 at 04:29:39ID: 2396398

Of course, if you're printing paychecks, it really woudln't matter how slow your function is.  It also shouldn't matter if you can handle numbers over a billion.  At least not for my paycheck.   :)

Wes

 

by: wesleystewartPosted on 2000-01-28 at 04:33:24ID: 2396413

OK, it can't get any faster than this.  This function (there are actually two.  The first one calls the second) is from the VBA developer's handbook.  It takes less than 100ms to run my test described above.  Mr. Getz certainly knows his stuff:

Function dhNumToStr(ByVal varValue As Variant) As String
    ' From "VBA Developer's Handbook"
    ' by Ken Getz and Mike Gilbert
    ' Copyright 1997; Sybex, Inc. All rights reserved.
   
    ' Takes a number and converts it into text for writing
    ' checks. For example, 24.95 gets converted to
    ' Twenty-Four and 95/100
   
    ' In:
    '       varValue contains the number to be converted to text
    ' Out:
    '       Returns the string or an empty string on any error
   
    On Error GoTo HandleErrors
   
    Dim intTemp As Integer
    Dim varNames As Variant
    Dim lngDollars As Long
    Dim intCents As Integer
    Dim strOut As String
    Dim strTemp As String
    Dim intI As Integer
   
    If Not IsNumeric(varValue) Then Exit Function
   
    ' 999,999,999.99 is the largest possible value.
    If varValue > 999999999.99 Then Exit Function
    varNames = Array("", "Thousand", "Million")
   
    varValue = Abs(varValue)
    lngDollars = Int(varValue)
    intCents = (varValue - lngDollars) * 100

    If lngDollars > 0 Then
        ' Loop through each set of three digits,
        ' first the hundreds, then thousands, and then
        ' millions.
        Do
            intTemp = lngDollars Mod 1000
            lngDollars = Int(lngDollars / 1000)
            ' Prepend spelling of new triplet of digits to the
            ' existing output.
            If intTemp <> 0 Then
                strOut = dhHandleGroup(intTemp) & " " & _
                 varNames(intI) & " " & strOut
            End If
            intI = intI + 1
        Loop While lngDollars > 0
        ' Handle the cents.
        strOut = RTrim(strOut) & " and " & _
         Format$(intCents, "00") & "/100"
    End If
   
ExitHere:
    dhNumToStr = strOut
    Exit Function

HandleErrors:
    ' Handle all errors by returning an empty string
    strOut = ""
    Resume ExitHere
End Function

Private Function dhHandleGroup(ByVal intValue As Integer) As String
    ' From "VBA Developer's Handbook"
    ' by Ken Getz and Mike Gilbert
    ' Copyright 1997; Sybex, Inc. All rights reserved.
   
    ' Called by dhNumToStr
    Static varOnes As Variant
    Static varTens As Variant
    Dim strOut As String
    Dim intDigit As Integer
   
    If IsEmpty(varOnes) Then
        varOnes = Array("", "One", "Two", "Three", "Four", "Five", _
         "Six", "Seven", "Eight", "Nine", "Ten", _
         "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", _
         "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty")
    End If
    If IsEmpty(varTens) Then
        ' Elements 0 and 1 in this array aren't used.
        varTens = Array("", "", "Twenty", "Thirty", "Forty", _
         "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
    End If
   
    ' Get the hundreds digit, and then the rest.
    intDigit = intValue \ 100
    intValue = intValue Mod 100
   
    ' If there's a hundreds digit, add that now.
    If intDigit > 0 Then strOut = varOnes(intDigit) & " Hundred"
           
    ' Handle the tens and ones digits.
    Select Case intValue
        Case 1 To 20
            strOut = strOut & varOnes(intValue)
        Case 21 To 99
            intDigit = intValue \ 10
            intValue = intValue Mod 10
            If intDigit > 0 Then
                strOut = strOut & " " & varTens(intDigit)
            End If
            If intValue > 0 Then
                strOut = strOut & "-" & varOnes(intValue)
            End If
    End Select

    dhHandleGroup = strOut
End Function



 

by: CHiLiNVLnPosted on 2000-01-28 at 10:58:52ID: 2397672

Thanks fpr your help. I really appreciate it!  Take care!

 

by: DedushkaPosted on 2000-01-28 at 18:53:15ID: 2402901

CHiLiNVLn,
Well done! Let me shake your hand!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...