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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Sargent
    New Member
    • Jan 2011
    • 6

    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.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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!

    Comment

    • Steven Sargent
      New Member
      • Jan 2011
      • 6

      #3
      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.

      Comment

      • john garvey
        New Member
        • Jan 2011
        • 50

        #4
        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(Pa yNum 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(Pay Num)


        End Function

        Public Function PoundsPence(Pay Num 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(PayNu m, Nt)
        Else
        Po = Left(PayNum, start - 1)
        pence = Val(Right(PayNu m, Len(PayNum) - (Len(Po) + 1)))
        PayNum = Po
        Po = getPounds(PayNu m, Nt)
        If getDigitsToText (pence, Nt) Then
        Pe = Nt & " pence"
        Pe = Replace(Pe, " ", " ")
        End If
        End If
        PoundsPence = Po & Pe
        PoundsPence = UCase(Left(Poun dsPence, 1)) & LCase(Right(Pou ndsPence, Len(PoundsPence ) - 1))
        Exit_PoundsPenc e:
        Exit Function
        Err_PoundsPence :
        MsgBox Error$
        Resume Exit_PoundsPenc e
        End Function

        Private Function getPounds(PayNu m 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(getPoun ds, " -", "-")
        getPounds = Replace(getPoun ds, " ", " ")

        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(iUni t)
        End If
        End Select
        getDigitsToText = (iNum > 0)
        End Function

        Comment

        Working...