Oh my god. It's full of code!

Coldfusion number to text string function

Hey all,

I’ve been doing some work with phone systems that read text. One issue I hit is that they tend to want to read numbers as…. well numbers. For example an address like 2324 nowhwere street would come out sounding like ‘two thousand twenty-four nowhere street’. While correct, it’s not exactly what a caller wants to hear. So I threw together this quick function that will take a string and convert any numbers into their word equivalent. Hope this helps someone out there.

<cffunction name="numToText" hint="convert a number into text for the IVR system" returntype="string">
    <cfargument name="number" type="string" required="yes" hint="A string that contains numbers to convert">
    
    <cfset var returnString = "">
    
    <cfloop from="1" to="#len(arguments.number)#" index="i">
        <cfif isnumeric(mid(arguments.number,i,1))>
            <cfswitch  expression="#mid(arguments.number,i,1)#">
                <cfcase value="0">
                    <cfset appendString = "zero">
                </cfcase>
         
                <cfcase value="1">
                    <cfset appendString = "one">
                </cfcase>
        
                <cfcase value="2">
                    <cfset appendString = "two">
                </cfcase>
                
                <cfcase value="3">
                    <cfset appendString = "three">
                </cfcase>                        
        
                <cfcase value="4">
                    <cfset appendString = "four">
                </cfcase>
         
                <cfcase value="5">
                    <cfset appendString = "five">
                </cfcase>
        
                <cfcase value="6">
                    <cfset appendString = "six">
                </cfcase>
                
                <cfcase value="7">
                    <cfset appendString = "seven">
                </cfcase>
         
                 <cfcase value="8">
                    <cfset appendString = "eight">
                </cfcase>
                
                <cfcase value="9">
                    <cfset appendString = "nine">
                </cfcase> 
            </cfswitch> 
                <cfset returnString = returnString & " " & appendString>
            <cfelse>
                 <cfset appendString = mid(arguments.number,i,1)>
                 <cfset returnString = returnString & appendString>
        </cfif>   
                      
    </cfloop>
    
    <cfreturn returnString>
</cffunction>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s