Print

Zoho Deluge - Pad with leading Zeros

What?
There may be other articles out there documenting this but I can't keep trying to find these in Google so referring to my own website, this is a doddle for me to find each time.

Why?
I have to do this so often that it deserves an article. Though writing a whole page for this is probably unwarranted so I may rename this article later to common things I need to do in Zoho Deluge.

How?
So going through the forums you may find the following example:
copyraw
string padWithLeadingZeros(int finalStringLength, int startingNumber)
{
      return leftpad(toString(input.startingNumber), input.finalStringLength).replaceAll(" ", "0");
}
  1.  string padWithLeadingZeros(int finalStringLength, int startingNumber) 
  2.  { 
  3.        return leftpad(toString(input.startingNumber), input.finalStringLength).replaceAll(" ", "0")
  4.  } 

This looks like pretty old code and not wanting to cause offence, it isn't the JavaScript style or chain I like which appends the functions instead and is clearer for me personally to understand:
copyraw
// I want 010

v_NextIndex = 10;
// yields 10

v_NextIndexStr = v_NextIndex.leftpad(3);
// yields " 10"

v_NextIndexStr = v_NextIndex.leftpad(3).replaceAll(" ", "0");
// yields "010"
  1.  // I want 010 
  2.   
  3.  v_NextIndex = 10
  4.  // yields 10 
  5.   
  6.  v_NextIndexStr = v_NextIndex.leftpad(3)
  7.  // yields " 10" 
  8.   
  9.  v_NextIndexStr = v_NextIndex.leftpad(3).replaceAll(" ", "0")
  10.  // yields "010" 

Source(s):
Category: Zoho :: Article: 705