Connecting Tech Pros Worldwide Help | Site Map

count digits

Newbie
 
Join Date: Aug 2008
Posts: 3
#1: Aug 6 '08
Hi Guys,

I need to make a pin like ADD - 00001 through ADD - 99999. I used the ID colomn in the table to generate this number like below:

<cfset #Pin# = #RIGHT('ADD - 0000'& #MyQuery.id#,13)#>

Now I have problem replacing the 0000 with numbers without loosing the 5 digit format. for example if the id value is 29 my pin should be ADD - 00029. any Idea?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Aug 6 '08

re: count digits


Make a pad UDF which adds the required number of zeros and use that:
Expand|Select|Wrap|Line Numbers
  1. <cfset Pin = 'ADD - ' & pad(MyQuery.id,0,5)>
Newbie
 
Join Date: Aug 2008
Posts: 3
#3: Aug 6 '08

re: count digits


Never mind. I fixed it. I'd love to post it if someone else has the same problem just let me know.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Aug 6 '08

re: count digits


Just post it anyway. You never know when it might prove useful to someone.
Newbie
 
Join Date: Aug 2008
Posts: 3
#5: Aug 6 '08

re: count digits


I used very simple cfif statement and the same cfset as below;
Expand|Select|Wrap|Line Numbers
  1. <cfif #myQuery.id# LT 10 >
  2.         <cfset #Pin# = #RIGHT('ADD - 0000'& #myQuery.id#,16)#>
  3.      <cfelseif #MasterQuery.id# LT 100 >
  4.         <cfset #Pin# = #RIGHT('ADD - 000'& #myQuery.id#,16)#>
  5.       <cfelse>
  6.         <cfset #Pin# = #RIGHT('ADD - 00'& #myQuery.id#,16)#>
  7.       </cfif>
  8.  
But I'm working on your suggestion, because I saw it after I wrote my simple if statement. Here is the UDF so far. Thanks anyway.


Expand|Select|Wrap|Line Numbers
  1. function PadString(myQuery.id, 0, 5)
  2. {
  3.   Var Padding = RepeatString(0, 5);
  4.   return Padding & string;
  5. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Aug 6 '08

re: count digits


You'll want to replace myQuery.id, 0 and 5 with variables. Also, your padding function won't work as desired because you've not made a check on the length of the string passed. You'll want to get the length of the string and subtract that from the pad-length to pass to RepeatString().
Reply