473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,326 developers and data experts.

Converting numbers to Words

missinglinq
3,532 Expert 2GB
From the Access Object Dialog Box click on "Modules"
Click on new to make a new module
Copy then paste the following code into the new module:

Expand|Select|Wrap|Line Numbers
  1. Function SayNo(ByVal N As Currency) As String
  2.  
  3.     Const Thousand = 1000@
  4.     Const Million = Thousand * Thousand
  5.     Const Billion = Thousand * Million
  6.     Const Trillion = Thousand * Billion
  7.  
  8.     If (N = 0@) Then SayNo = "zero": Exit Function
  9.  
  10.     Dim Buf As String: If (N < 0@) Then Buf = "negative " Else Buf = ""
  11.     Dim Frac As Currency: Frac = Abs(N - Fix(N))
  12.     If (N < 0@ Or Frac <> 0@) Then N = Abs(Fix(N))
  13.     Dim AtLeastOne As Integer: AtLeastOne = N >= 1
  14.  
  15.     If (N >= Trillion) Then
  16.         Debug.Print N
  17.         Buf = Buf & SayNoDigitGroup(Int(N / Trillion)) & " trillion"
  18.         N = N - Int(N / Trillion) * Trillion
  19.         If (N >= 1@) Then Buf = Buf & " "
  20.     End If
  21.  
  22.     If (N >= Billion) Then
  23.         Debug.Print N
  24.         Buf = Buf & SayNoDigitGroup(Int(N / Billion)) & " billion"
  25.         N = N - Int(N / Billion) * Billion
  26.         If (N >= 1@) Then Buf = Buf & " "
  27.     End If
  28.  
  29.     If (N >= Million) Then
  30.         Debug.Print N
  31.         Buf = Buf & SayNoDigitGroup(N \ Million) & " million"
  32.         N = N Mod Million
  33.         If (N >= 1@) Then Buf = Buf & " "
  34.     End If
  35.  
  36.     If (N >= Thousand) Then
  37.         Debug.Print N
  38.         Buf = Buf & SayNoDigitGroup(N \ Thousand) & " thousand"
  39.         N = N Mod Thousand
  40.         If (N >= 1@) Then Buf = Buf & " "
  41.     End If
  42.  
  43.     If (N >= 1@) Then
  44.         Debug.Print N
  45.         Buf = Buf & SayNoDigitGroup(N)
  46.     End If
  47.  
  48.     If (Frac = 0@) Then
  49.         Buf = Buf
  50.     ElseIf (Int(Frac * 100@) = Frac * 100@) Then
  51.         If AtLeastOne Then Buf = Buf & " and "
  52.         Buf = Buf & Format$(Frac * 100@, "00") & "/100"
  53.     Else
  54.         If AtLeastOne Then Buf = Buf & " and "
  55.         Buf = Buf & Format$(Frac * 10000@, "0000") & "/10000"
  56.     End If
  57.  
  58.     SayNo = Buf
  59. End Function
  60.  
  61. Private Function SayNoDigitGroup(ByVal N As Integer) As String
  62.  
  63.     Const Hundred = " hundred"
  64.     Const One = "one"
  65.     Const Two = "two"
  66.     Const Three = "three"
  67.     Const Four = "four"
  68.     Const Five = "five"
  69.     Const Six = "six"
  70.     Const Seven = "seven"
  71.     Const Eight = "eight"
  72.     Const Nine = "nine"
  73.     Dim Buf As String: Buf = ""
  74.     Dim Flag As Integer: Flag = False
  75.  
  76.     Select Case (N \ 100)
  77.         Case 0: Buf = "": Flag = False
  78.         Case 1: Buf = One & Hundred: Flag = True
  79.         Case 2: Buf = Two & Hundred: Flag = True
  80.         Case 3: Buf = Three & Hundred: Flag = True
  81.         Case 4: Buf = Four & Hundred: Flag = True
  82.         Case 5: Buf = Five & Hundred: Flag = True
  83.         Case 6: Buf = Six & Hundred: Flag = True
  84.         Case 7: Buf = Seven & Hundred: Flag = True
  85.         Case 8: Buf = Eight & Hundred: Flag = True
  86.         Case 9: Buf = Nine & Hundred: Flag = True
  87.     End Select
  88.  
  89.     If (Flag <> False) Then N = N Mod 100
  90.     If (N > 0) Then
  91.         If (Flag <> False) Then Buf = Buf & " "
  92.     Else
  93.         SayNoDigitGroup = Buf
  94.         Exit Function
  95.     End If
  96.  
  97.     Select Case (N \ 10)
  98.         Case 0, 1: Flag = False
  99.         Case 2: Buf = Buf & "twenty": Flag = True
  100.         Case 3: Buf = Buf & "thirty": Flag = True
  101.         Case 4: Buf = Buf & "forty": Flag = True
  102.         Case 5: Buf = Buf & "fifty": Flag = True
  103.         Case 6: Buf = Buf & "sixty": Flag = True
  104.         Case 7: Buf = Buf & "seventy": Flag = True
  105.         Case 8: Buf = Buf & "eighty": Flag = True
  106.         Case 9: Buf = Buf & "ninety": Flag = True
  107.     End Select
  108.  
  109.     If (Flag <> False) Then N = N Mod 10
  110.     If (N > 0) Then
  111.         If (Flag <> False) Then Buf = Buf & "-"
  112.     Else
  113.         SayNoDigitGroup = Buf
  114.         Exit Function
  115.     End If
  116.  
  117.     Select Case (N)
  118.         Case 0:
  119.         Case 1: Buf = Buf & One
  120.         Case 2: Buf = Buf & Two
  121.         Case 3: Buf = Buf & Three
  122.         Case 4: Buf = Buf & Four
  123.         Case 5: Buf = Buf & Five
  124.         Case 6: Buf = Buf & Six
  125.         Case 7: Buf = Buf & Seven
  126.         Case 8: Buf = Buf & Eight
  127.         Case 9: Buf = Buf & Nine
  128.         Case 10: Buf = Buf & "ten"
  129.         Case 11: Buf = Buf & "eleven"
  130.         Case 12: Buf = Buf & "twelve"
  131.         Case 13: Buf = Buf & "thirteen"
  132.         Case 14: Buf = Buf & "fourteen"
  133.         Case 15: Buf = Buf & "fifteen"
  134.         Case 16: Buf = Buf & "sixteen"
  135.         Case 17: Buf = Buf & "seventeen"
  136.         Case 18: Buf = Buf & "eighteen"
  137.         Case 19: Buf = Buf & "nineteen"
  138.     End Select
  139.  
  140.     SayNoDigitGroup = Buf
  141.  
  142. End Function
  143.  
Save the module and call it, say, NumbersToWords

Now from your form call the function using the syntax

SayNo(YourNumericalValue)

As an example (since I don't know your skill level, sorry) here's what I did to test it. After doing the above to create the module, I ran up a simple form with a textbox and a label; txtTextNumbers and lblTextWords

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtTextNumbers_Exit(Cancel As Integer)
  2.      Me.lblTextWords.Caption = SayNo(Me.txtTextNumbers.Value)
  3. End Sub
  4.  
After entering your data and exiting the textbox the label will contain the word equivelent of the number entered.

The same code can be used in other events, of course.

The author is only known to me as M8KWR.
Jan 30 '07 #1
1 21436
puppydogbuddy
1,923 Expert 1GB
See also this link:

http://www.fabalou.com/Access/Modules/numbertowords.asp
Aug 11 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: fo | last post by:
Does anyone know if there is a free code available to convert numbers to text and/or text to numbers? Thanks
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
7
by: Converting Numeric to a word | last post by:
Hi, I have little experience in C#, Can anyone help me with this sproblem. " I want to convert a given number in a text to a word on a lable" for eg if we type 25 in text box , i want to display it...
7
by: MeganTSU | last post by:
Hey yall! I am trying to get this program finished for class.... It says that you are suppposed to write a program that will display a check formatted out put (the output looks like a check). I got...
3
by: bolly | last post by:
Hi, I've been putting Python data into a sqlite3 database as tuples but when I retrieve them they come back as unicode data e.g 'u(1,2,3,4)'.How can I change it back to a tuple so I can use it as...
2
by: Rohit111111 | last post by:
Hello all, I want to convert number into there equivalent words like if number is 567342 than result is Five Lac sixty seven thousand three hundred and fourty two. I am using asp.net c# Thanks...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
5
by: byte8bits | last post by:
Here's how I'm doing this right now, It's a bit slow. I've just got the code working. I was wondering if there is a more efficient way of doing this... simple example from interactive Python: ...
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
3
by: Bouzy | last post by:
I have a list of words and am trying to replace all the numbers in my list with whitespace. for word in words: numbers = re.search('+', word) word = clearup(word) if word in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.