Computer Science Canada

Code to Convert an Int into a Spoken Word String

Author:  GlobeTrotter [ Tue Nov 01, 2005 8:18 pm ]
Post subject:  Code to Convert an Int into a Spoken Word String

So you understand what this does, if you input "67" for example, the output will be "sixty seven". I didn't bother adding dashes because, they can become tricky and subjective. If anyone would like to try and shorten this code, please do, as I would benefit from that.

This can be very useful to simply import into your game and output numbers as text to the user for a better GUI.

Turing:


module IntToWords
    export Convert

    function stOnes (digit : int) : string
        case digit of
            label 1 :
                result "one"
            label 2 :
                result "two"
            label 3 :
                result "three"
            label 4 :
                result "four"
            label 5 :
                result "five"
            label 6 :
                result "six"
            label 7 :
                result "seven"
            label 8 :
                result "eight"
            label 9 :
                result "nine"
            label 0 :
                result "zero"
        end case
    end stOnes

    function stTeens (digit : int) : string
        case digit of
            label 4, 6, 7, 9 :
                result stOnes (digit) + "teen"
            label 1 :
                result "eleven"
            label 2 :
                result "twelve"
            label 3 :
                result "thirteen"
            label 5 :
                result "fifteen"
            label 8 :
                result "eighteen"
            label 0 :
                result "ten"
        end case
    end stTeens

    function stTens (digit : int) : string
        case digit of
            label 4, 6, 7, 9 :
                result stOnes (digit) + "ty"
            label 2 :
                result "twenty"
            label 3 :
                result "thirty"
            label 5 :
                result "fifty"
            label 8 :
                result "eighty"
        end case
    end stTens

    function stNumOfDigits (digit : int) : string
        case digit of
            label 0 :
                result stOnes (digit)
            label 1 :
                result stOnes (digit)
            label 2 :
                result stTeens (digit)
            label 3 :
                result "hundred"
            label 4 :
                result "thousand"
            label 7 :
                result "million"
            label 10 :
                result "billion"
        end case
    end stNumOfDigits

    %Sub-functiom
    function stLessThan100 (iNumber : int) : string
        var stOutput := ""
        if iNumber < 100 and iNumber > 0 then
            if iNumber < 10 then
                stOutput += stOnes (iNumber)
                result stOutput
            end if

            if iNumber < 20 then
                stOutput += stTeens (iNumber mod 10)
                result stOutput
            end if

            if iNumber < 100 then
                stOutput += stTens (iNumber div 10)
                if iNumber mod 10 ~= 0 then
                    stOutput += " " + stOnes (iNumber mod 10)
                end if
                result stOutput
            end if
        end if
    end stLessThan100

    function stLessThan1000 (iNumber : int) : string
        var stOutput := ""
        if iNumber < 1000 and iNumber >= 100 then
            stOutput += stOnes (iNumber div 100)
            stOutput += " " + stNumOfDigits (3)
            if iNumber mod 100 ~= 0 then
                stOutput += " and " + stLessThan100 (iNumber mod 100)
            end if
            result stOutput
        elsif iNumber > 0 and iNumber < 100 then
            result stLessThan100 (iNumber)
        end if
    end stLessThan1000

    function stLessThan1000000 (iNumber : int) : string
        var stOutput := ""
        if iNumber < 1000000 and iNumber >= 1000 then
            stOutput += stLessThan1000 (iNumber div 1000)
            stOutput += " " + stNumOfDigits (4)
            if iNumber mod 1000 ~= 0 then
                stOutput += ", " + stLessThan1000 (iNumber mod 1000)
            end if
            result stOutput
        elsif iNumber > 0 and iNumber < 100 then
            result stLessThan100 (iNumber)
        elsif iNumber >= 100 and iNumber < 1000 then
            result stLessThan1000 (iNumber)
        end if
    end stLessThan1000000

    function stLessThan1000000000 (iNumber : int) : string
        var stOutput := ""
        if iNumber < 1000000000 and iNumber >= 1000000 then
            stOutput += stLessThan1000000 (iNumber div 1000000)
            stOutput += " " + stNumOfDigits (7)
            if iNumber mod 1000000 ~= 0 then
                stOutput += ", " + stLessThan1000000 (iNumber mod 1000000)
            end if
            result stOutput
        elsif iNumber > 0 and iNumber < 100 then
            result stLessThan100 (iNumber)
        elsif iNumber >= 100 and iNumber < 1000 then
            result stLessThan1000 (iNumber)
        elsif iNumber >= 1000 and iNumber < 1000000 then
            result stLessThan1000000 (iNumber)
        end if
    end stLessThan1000000000

    %Main Function
    function Convert (iOriginalNumber : int) : string
        var stOutput : string := ""
        var iNumber : int := iOriginalNumber

        if iNumber < 0 then
            iNumber := abs (iNumber)
            stOutput += "negative "
        end if

        if iNumber = 0 then
            stOutput += stOnes (iNumber)
            result stOutput
        end if

        if iNumber < 100 then
            stOutput += stLessThan100 (iNumber)
            result stOutput
        end if

        if iNumber < 1000 then
            stOutput += stLessThan1000 (iNumber)
            result stOutput
        end if

        if iNumber < 1000000 then
            stOutput += stLessThan1000000 (iNumber)
            result stOutput
        end if

        if iNumber < 1000000000 then
            stOutput += stLessThan1000000000 (iNumber)
            result stOutput
        end if

        stOutput += stLessThan100 (iNumber div 1000000000)
        stOutput += " " + stNumOfDigits (10)
        %Error.Halt ("stOutput= " + stOutput + ", iNumber mod 1000000000= " + intstr (iNumber mod 1000000000))
        if iNumber mod 1000000000 ~= 0 then
            stOutput += ", " + stLessThan1000000000 (iNumber mod 1000000000)
        end if

        result stOutput
    end Convert
end IntToWords

setscreen("text")

var i : int
% Example implementation of the function.

for k : 1 .. 10
    randint(i, -maxint div 2, maxint div 2)
    put i
    put IntToWords.Convert (i)
end for


Author:  Cervantes [ Tue Nov 01, 2005 9:04 pm ]
Post subject: 

I've just skimmed the code, but I question why you used a class. I think a module would better suite this, since you don't want to create multiple objects of IntToStrings. Rather, you want a nice safe place for your code, and to be able to call your function whenever you wish.

Thanks for the submission! I remember when my compsci class was assigned this task. We had a day. Such terrible code was written that day. Razz

Author:  GlobeTrotter [ Tue Nov 01, 2005 9:19 pm ]
Post subject: 

I just wrote the code then threw it in the class. I'm not that familiar with modules, and I tried putting it into a module at first, I just couldn't really remember/figure out how to propery export the funciton so I put it in a class, which I'm more familiar with. It's essentially the same either way though.

Author:  [Gandalf] [ Wed Nov 02, 2005 12:31 am ]
Post subject: 

The syntax of modules and classes in Turing is really exactly the same. You export the same way in both. Smile

Author:  GlobeTrotter [ Wed Nov 02, 2005 4:39 pm ]
Post subject: 

Okay, thanks. I just edited my first post. Now it's in a module. I also realized that it didn't work for some numbers, I'm pretty confident there aren't any bugs anymore.

Author:  codemage [ Thu Nov 03, 2005 8:41 am ]
Post subject: 

Kind of a mundane, yet useful program. :/

You could use it for printing cheques, ie, with a few quick mods.

One hundred five dollars and thirty eight cents. Smile


: