Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coldfusion convert number to text

I have a coldfusion app in which I calculate the number remaining of a certain object.

So I have a integer... like 9.

But I need to print it to the screen in text form.... like nine.

Is there a built in function to do this? I googled and couldn't find one.

like image 468
lovefaithswing Avatar asked Feb 10 '10 23:02

lovefaithswing


3 Answers

Steven is correct, the direct answer is that there is no built in function for this but here is a UDF you can play with NumberAsString

like image 187
kevink Avatar answered Nov 16 '22 05:11

kevink


No. I'm afraid there is no built in function for this.

You'd need to write a user defined function or method in a cfc to do this for you.

like image 22
Stephen Moretti Avatar answered Nov 16 '22 06:11

Stephen Moretti


Here's how I did it, after much effort, I might add. I wrote this under ColdFusion 4.5, so your mileage may vary.

The key is breaking the digits into 3-number chunks, then displaying each chunk with the respective addenuum (million, thousands, etc.), and of course, dealing with random zeros and numbers in the teens. The first lists can be used to change languages, but require the order be right (i.e. first entry is = 1). This was for a check display, where the numeral had to be written out in english, therefore the plethora of 'check' variables. The variable you're converting is a numeral named 'check_amount'.

I'll apologize up front for the lousy code - I'm a designer, not a programmer. There are so many repeated sections that should be refactored, but it's for dealing with leading zeros and teens, mostly.

Third edit is the charm, I suppose. This final (working) version now handles zero dollars properly.

<cfoutput><cfif IsNumeric(check_amount)> <!--- is it a number? --->
<cfparam name="write_single" default="one,two,three,four,five,six,seven,eight,nine, ">
<cfparam name="write_double" default=" ,twenty,thirty,fourty,fifty,sixty,seventy,eighty,ninety">
<cfparam name="teens" default="11,12,13,14,15,16,17,18,19">
<cfparam name="teens_written" default="eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen">
<cfparam name="triplet_after" default="hundred, thousand, million, billion, trillion, quadrillion, quintillion, hexillion, heptillion, octillion, nonillion, decillion, unodecillion, duodecillion">

<cfset x=#ListLen(DecimalFormat(check_amount))#>
<!--- seperate the number into sections, using the built-in Decimal Format to make it into a list of 3-digit numbers --->
<cfloop list="#DecimalFormat(check_amount)#" index="trips" delimiters=",">
<!--- seperate the number into hundreds tens and singles, making the teens exception --->
<cfif #Evaluate(Int(trips))# NEQ "0">
    <cfif Len(Int(trips)) EQ "3">
        <cfif mid(Int(trips), 1, 1) EQ "0">
            <cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0">
                #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')#
            <cfelse>
                #listGetAt(write_double, mid(Int(trips), 2, 1), ',')#
                <cfif mid(Int(trips), 3, 1) NEQ "0">
                    #listGetAt(write_single, mid(Int(trips), 3, 1), ',')#
                </cfif>
            </cfif>
        <cfelse>
            #listGetAt(write_single, mid(Int(trips), 1, 1), ',')# #listGetAt(triplet_after, 1, ',')#
        </cfif>
        <cfif mid(trips, 2, 1) NEQ "0">
            <cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0">
                #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')#
            <cfelse>
                #listGetAt(write_double, mid(Int(trips), 2, 1), ',')#
                <cfif mid(trips, 3, 1) NEQ "0">
                    #listGetAt(write_single, mid(Int(trips), 3, 1), ',')#
                </cfif>
            </cfif>
        <cfelse>
            <cfif mid(trips, 3, 1) NEQ "0">
                #listGetAt(write_single, mid(Int(trips), 3, 1), ',')#
            </cfif> 
        </cfif> 
    <cfelseif Len(Int(trips)) EQ "2" AND mid(Int(trips), 1, 1) NEQ "0">
        <cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0">
            #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')#
        <cfelse>
            #listGetAt(write_double, mid(Int(trips), 1, 1), ',')#
            <cfif mid(trips, 2, 1) NEQ "0">
                #listGetAt(write_single, mid(Int(trips), 2, 1), ',')#
            </cfif>
        </cfif>
    <cfelseif Len(Int(trips)) EQ 1 AND mid(int(trips), 1, 1) NEQ "0">
        #listGetAt(write_single, mid(Int(trips), 1, 1), ',')# 
    </cfif>
        <!--- deal with the thousands and millions seperators, doesn't include hundreds on last loop --->
        <cfif x NEQ "1">#listGetAt(triplet_after, x, ',')#</cfif>
<cfelse>
    <!--- Zero Dollars? How about... ---><cfif x EQ #ListLen(DecimalFormat(check_amount))#> No </cfif> 
</cfif>
<cfset x=x-1><!--- next loop, next valuations --->
</cfloop>
<!--- output tailing text and cents in check format --->
Dollars and <sup>#right(DecimalFormat(check_amount), 2)#</sup>/<sub>100</sub> cents</p>
</cfif></cfoutput>
like image 1
Andrew Avatar answered Nov 16 '22 04:11

Andrew