Add Leading Zeros to a Numeric Value with ColdFusion

ColdFusion's numberformat() allows for the use of '_' (underscore) or '9' as a placeholder in a numeric mask. But these do not add a leading zero to non-decimal values. ( If the number contains fewer digits than the mask, you get the truncated number without any zeros on the left side).

In some cases, however, you want those zeros. The super-simple CF solution? Use a '0' as the mask character!


<cfset theNumber = "1287">
<cfoutput>#NumberFormat(TheNumber, "000009")#</cfoutput>

This returns 001287


<cfset theNumber = "1287">
<cfoutput>#NumberFormat(TheNumber, "999999")#</cfoutput>

This returns 1287

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista