Wednesday, May 31, 2023

D365 F&O// X++: Adding zero numbers into Left/Right text values

 

Sometimes we need to add padding numbers in the files in AX. Ex: Postive pay files
I found the below post that helped to add the padding zero numbers in the positive pay file.

Original post: http://www.atomicax.com/content/zero-padding-numbers

X++ provides us with an easy way to pad strings with extra characters. A common situation where this is required is use a "fake" number sequence inside AX.
If you wish to increment a counter, but store or display it as a string such as "ABC-00001", "ABC-00002" etc then padding the counter with zeros is required.
We can use the strRFix() and strLFix() methods to achieve this

static void testZeroPadding(Args _args)
{
    int i = 1;
    str padded;
    str finalResult;
    ;
    // Create a string, with a length of 5, ending with the value of i and padded with zeros
    padded = strRFix(int2str(i), 5, "0");
    finalResult = strFmt("ABC-%1", padded);

}

Result will be displayed ABC-00001. If we use strLFix instead of strRFix, it will pad to the right with zeros, giving us ABC-10000

Another way, also common thoughts, the approach is below sample

static str zeroPad(str item, int numZero)
{
    int i;
    str out;
    ;
    for(i=0;i<numZero-strlen(item); i++)
    {
        out += "0";
    }

    return(out + item);
}

 

No comments:

Post a Comment

D365 FinOps - How call a runnable class with URL parameters

We are already familiar with calling D365FO URL menu items or forms or Runnable class. To recap, I am giving few samples below To call  Disp...