jayron
jayron

Reputation: 69

How do I format an integer using Coldfusion?

I'm trying to use NumberFormat, on an integer, in order to pad it with leading zeroes. Unfortunately, NumberFormat seems to have no affect on the integer. Any advice would be appreciated.

 <cffunction name="myFunction" access="remote" returntype="numeric">
    <cfargument name="myId" type="numeric" required="yes" />
        <cfquery name="myQuery">
            SELECT COUNT(fileid) AS itemCount FROM files
            WHERE directory = '#myId#'
        </cfquery>
        <cfset newId = numberFormat( myQuery.itemCount, '0000000' )>
        <cfreturn newId />
</cffunction>

Upvotes: 1

Views: 340

Answers (2)

Chris Tierney
Chris Tierney

Reputation: 1539

Change your return type to a string. I'm pretty sure numeric will truncate leading zeros. Also, as a good practice, addoutput="false" to all your <cfcomponent> and <cffunction> tags.

Upvotes: 11

mrehmsluzi
mrehmsluzi

Reputation: 33

Try using Val() instead.

<cfset newId = Val(myQuery.itemCount)>

Upvotes: -4

Related Questions