Using RepeatString to create a mask in NumberFormat
Today I was working on creating a CSV file that required numeric columns to be a fixed number of digits, using leading zeros to make up the difference. So, for example, if the column required 5 digits, the number 123 should show up as 00123. This is super simple using the NumberFormat() function in CFML, simply specify a mask like so:
<cfoutput>#variables.myNum#</cfoutput>
But, in my particular case, I had several columns that required 12 and 15 digit numbers. Now, rather than go through bunch of NumberFormat() functions and count out 12 or 15 zeros, I came up with this little gem:
<cfoutput>#variables.myNum#</cfoutput>
I simply used the RepeatString() function to repeat "0" 15 times, and I used that to populate the mask of my NumberFormat() function. So, the above code renders the exact same thing as NumberFormat(123,'000000000000000'). The beauty of this is that, at a glace, you can tell how may zeros are in your NumberFormat mask, without having to constantly count them.


http://cflib.org/udf/PadStringToLen
Carl