Skip to content

Instantly share code, notes, and snippets.

@geirman
Last active June 15, 2021 15:16
Show Gist options
  • Save geirman/eb0302e242c13755a4f2 to your computer and use it in GitHub Desktop.
Save geirman/eb0302e242c13755a4f2 to your computer and use it in GitHub Desktop.
How to Pad a String with Zeros in ColdFusion
<cfscript>
/*
Try It Here:
http://www.trycf.com/scratch-pad/gist/eb0302e242c13755a4f2
*/
writeOutput( pad('123', 5) );
function pad( str, n ){
return replace( RJustify( str, n ), ' ', '0', 'all' );
}
</cfscript>
@JamoCA
Copy link

JamoCA commented Jun 5, 2018

You may want to specifically cast it as a string... you never know when ColdFusion will interpret values as numeric and drop the zeros.

return JavaCast("string", replace( RJustify( str, n ), ' ', '0', 'all' ) );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment