Oh my god. It's full of code!

Posts tagged “numeric

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 value="1">
                    <cfset appendString = "one">
                <cfcase value="2">
                    <cfset appendString = "two">
                <cfcase value="3">
                    <cfset appendString = "three">
                <cfcase value="4">
                    <cfset appendString = "four">
                <cfcase value="5">
                    <cfset appendString = "five">
                <cfcase value="6">
                    <cfset appendString = "six">
                <cfcase value="7">
                    <cfset appendString = "seven">
                 <cfcase value="8">
                    <cfset appendString = "eight">
                <cfcase value="9">
                    <cfset appendString = "nine">
                <cfset returnString = returnString & " " & appendString>
                 <cfset appendString = mid(arguments.number,i,1)>
                 <cfset returnString = returnString & appendString>
    <cfreturn returnString>