Using Rept to pad zeroes

I was creating a PeopleCode script to populate test persons in my database. I needed the persons to have IDs that would be padded with 0s where necessary. For example:

Solution:
1. Determine how many people you are creating (eg. 100).
2. Find the number of digits of that number (eg. 100 has 3 digits)
3. Subtract that from the number of the person that you’re currently trying to create an id for (eg. 50 has 2 digits, so 3 – 2 = 1). This will give you the number of zeroes you’ll need to left pad for the id.
4. Encapsulate this number in the PeopleCode Rept function to pad the 0s:

[sourcecode language=”peoplecode”]
Local number &maxIDsToCreate = 100;
Local number &currentIDNumber = 50;
Local string &currentID = "JM" | Rept("0", (Len(String(&maxIDsToCreate)) – Len(String(&currentIDNumber)))) | &currentIDNumber;
[/sourcecode]


Warning: Missing argument 1 for cwppos_show_review(), called in /var/www/jaymathew.com/wp-content/themes/flat/content-single.php on line 29 and defined in /var/www/jaymathew.com/wp-content/plugins/wp-product-review/includes/legacy.php on line 18

1 comment

  1. Or you could just do it yourself once the string is returned simply cast it to the datatype you want:

    /* SET THESE UP IN SEPARATE CLASSES UNDER ONE APP PACKAGE.*/
    /* LPAD FUNCTION TO LEFT PAD AN EXISTING STRING TO A SPECIFIED LENGTH */
    class Cad_Lpad
    method Cad_Lpad();
    method lpad(&lSTRING As string, &lLENGTH As number, &lCHAR As string) Returns any;

    end-class;

    method Cad_Lpad

    end-method;

    method lpad
    /+ &lSTRING as String, +/
    /+ &lLENGTH as Number, +/
    /+ &lCHAR as String +/
    /+ Returns Any +/
    Local number &lLEN, &lL;
    &lLEN = Len(&lSTRING);
    For &lL = &lLEN To (&lLENGTH – 1)
    &lSTRING = &lCHAR | &lSTRING;
    End-For;
    Return &lSTRING;
    end-method;

    /* RPAD FUNCTION TO RIGHT PAD AN EXISTING STRING TO A SPECIFIED LENGTH */
    class Cad_Rpad
    method Cad_Rpad();
    method rpad(&rSTRING As string, &rLENGTH As number, &rCHAR As string) Returns any;

    end-class;

    method Cad_Rpad

    end-method;

    method rpad
    /+ &rSTRING as String, +/
    /+ &rLENGTH as Number, +/
    /+ &rCHAR as String +/
    /+ Returns Any +/
    Local number &rLEN, &rL;
    &rLEN = Len(&rSTRING);
    For &rL = &rLEN To (&rLENGTH – 1)
    &rSTRING = &rSTRING | &rCHAR;
    End-For;
    Return &rSTRING;
    end-method;

Leave a Reply

Your email address will not be published. Required fields are marked *

*