473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How do I convert a number in to its written form?

How can I convert a number into its written form?
For example, I input 15, and the program will output "fifteen".
I'm doing this in a console application. It would be better if, instead of giving me the full answer, you could set me on the right path.

All I can think of now is a very long switch structure for every character in the number.
Jan 14 '11 #1
3 2351
GaryTexmo
1,501 Expert 1GB
I don't think you need a giant switch statement... You can lay down some rules and check for compliance:

* 0-9 have a specific equivalent word associated with them
* 10 added to any of the first adds postfix of "teen", with the exception of 11 and 12 which are single mapping
* 20, 30, ..., 90 added to any of the first adds a prefix of the word equivalent of those
* 100, 200, ..., 900 added to the above set also adds another prefix

Then I'd start parsing the number by digit and digit sets, trying to fit pieces of them into the above categories and building the word string as I can. You can check the length of the number or subset to see what prefix you should generate, perhaps?

I don't know if this is the correct way or not, I've never done this before and you've asked for a nudge. If I were to approach this problem, I believe this is how I'd start.

I hope that helps!
Jan 15 '11 #2
Actually, that's exactly what I had considered first... I don't think I explained myself well.

At any rate, I was hoping there was a quicker way that I wasn't seeing.

Thanks.
Jan 15 '11 #3
Here is a solution I wrote in VB which will convert any money value below 99,999,999.99 into text which will be gramatically correct. You could butcher this to get what you need

' PayNum is a monetry value, in this case £GBPounds, with or without decimal places for pence
' The returned string is gramatically correct, as if you wrote a cheque (check)



Option Explicit
Dim strUnder21(1 To 20) As String
Dim strTens(1 To 9) As String


Public Function NumToConvert(PayNum As String) As String

' Initialise the arrays
strUnder21(1) = "One "
strUnder21(2) = "Two "
strUnder21(3) = "Three "
strUnder21(4) = "Four "
strUnder21(5) = "Five "
strUnder21(6) = "Six "
strUnder21(7) = "Seven "
strUnder21(8) = "Eight "
strUnder21(9) = "Nine "
strUnder21(10) = "Ten "
strUnder21(11) = "Eleven "
strUnder21(12) = "Twelve "
strUnder21(13) = "Thirteen "
strUnder21(14) = "Fourteen "
strUnder21(15) = "Fifteen "
strUnder21(16) = "Sixteen "
strUnder21(17) = "Seventeen"
strUnder21(18) = "Eighteen "
strUnder21(19) = "Nineteen "
strUnder21(20) = "Twenty "

strTens(1) = "Ten "
strTens(2) = "Twenty "
strTens(3) = "Thirty "
strTens(4) = "Fourty "
strTens(5) = "Fifty "
strTens(6) = "Sixty "
strTens(7) = "Seventy "
strTens(8) = "Eighty "
strTens(9) = "Ninety "

NumToConvert = PoundsPence(PayNum)


End Function

Public Function PoundsPence(PayNum As String) As String
Dim Po As String, Pe As String, pence As Integer
Dim start As Integer, Nt As String
On Error GoTo Err_PoundsPence
start = InStr(PayNum, ".")
If start = 0 Then
Po = getPounds(PayNum, Nt)
Else
Po = Left(PayNum, start - 1)
pence = Val(Right(PayNum, Len(PayNum) - (Len(Po) + 1)))
PayNum = Po
Po = getPounds(PayNum, Nt)
If getDigitsToText(pence, Nt) Then
Pe = Nt & " pence"
Pe = Replace(Pe, " ", " ")
End If
End If
PoundsPence = Po & Pe
PoundsPence = UCase(Left(PoundsPence, 1)) & LCase(Right(PoundsPence, Len(PoundsPence) - 1))
Exit_PoundsPence:
Exit Function
Err_PoundsPence:
MsgBox Error$
Resume Exit_PoundsPence
End Function

Private Function getPounds(PayNum As String, Nt As String) As String
Dim Tm As Integer, Tms As String
Dim Ht As Integer, Hts As String
Dim tt As Integer, tts As String
Dim H As Integer, Hs As String
Dim t As Integer, ts As String
On Error GoTo Err_NumToText

Nt = Right("00000000" & PayNum, 8)
Tm = Val(Mid(Nt, Len(Nt) - 8 + 1, 2))
Ht = Val(Mid(Nt, Len(Nt) - 6 + 1, 1))
tt = Mid(Nt, Len(Nt) - 5 + 1, 2)
H = Val(Mid(Nt, Len(Nt) - 3 + 1, 1))
t = Val(Mid(Nt, Len(Nt) - 2 + 1, 2))

' Millions
If getDigitsToText(Tm, Tms) Then
Tms = Tms & "million "
End If
' Hundred Thousands
If getDigitsToText(Ht, Hts) Then
Hts = Hts & "hundred "
End If
' Ten thousands/ thousands
If getDigitsToText(tt, tts) Then
If Ht > 0 Then tts = "and " & tts
End If
If (Ht > 0) Or (tt > 0) Then
tts = tts & "thousand "
End If
' Hundreds
If getDigitsToText(H, Hs) Then
Hs = Hs & "hundred "
End If
' Tens and units
If getDigitsToText(t, ts) Then
If (Tm > 0) Or (Ht > 0) Or (tt > 0) Or (H > 0) Then ts = "and " & ts
End If
getPounds = Tms & Hts & tts & Hs & ts
getPounds = Replace(getPounds, " -", "-")
getPounds = Replace(getPounds, " ", " ")

If Len(getPounds & "") > 0 Then
getPounds = getPounds & "pounds "
End If

Exit_NumToText:
Exit Function
Err_NumToText:
MsgBox Error$
Resume Exit_NumToText
End Function

Private Function getDigitsToText(iNum As Integer, txtAmount As String) As Boolean
Dim iUnit As Integer
iUnit = iNum Mod 10
Select Case iNum
Case 0
txtAmount = ""
Case Is < 21
txtAmount = strUnder21(iNum)
Case Else
txtAmount = strTens((iNum - (iNum Mod 10)) / 10)
If iUnit > 0 Then
txtAmount = txtAmount & "-" & strUnder21(iUnit)
End If
End Select
getDigitsToText = (iNum > 0)
End Function
Jan 15 '11 #4

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

Similar topics

2
by: Enjoy Life | last post by:
I am making a check writing page and was wondering how to convert a variable in the form of $1234.56 to One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents (Or something like that). ...
4
by: Utada P.W. SIU | last post by:
I know that MS have limited the form data size which about 100k however, I need submit a data that may be less or more than 100k, such as 500k I have found a code that using...
3
by: R. Hopping | last post by:
What's the easiest way to convert an access form to to a web based Front end ? Is there any software that can convert these formats easily? If not any Beginer tutorials would be helpful. ...
2
by: Billy Greening | last post by:
I have an application in C# that was written as a bunch of individual CS files. I would like to combine these all into single solution that can be editied with the Visual Studio 2003 editor. Does...
0
by: sevenlives | last post by:
Hey ... Can you help me out with this error? <!----------------------------------> Microsoft OLE DB Provider for SQL Server error '80040e07' Error converting data type varchar to bigint....
2
by: Parasyke | last post by:
Please help.... I have a form that I successfully add records to that I want to copy and turn into a form for editing records from that same table (It is imperitive that it be done this way, rather...
2
by: mktilu | last post by:
hi can any one tell do we have any function to convert number to text.For example 25 to be converted to twenty five in SQL
1
by: nileshp | last post by:
How to convert XSD schema form to XDR schema form?
2
by: nineballssj9 | last post by:
#include <stdio.h> #include <string.h> #include <math.h> //i'm trying to convert number into word like for an example if i type //99.99 then it will print ninety nine point ninety nine //can...
1
by: Rohullah | last post by:
Hello I want to convert emp table sal column to word form for every row in emp table. how to do it. iam traing this code but it doesnt work i dont know why. on when-new-form-instance...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.