Tuesday, November 9, 2021

Zero Padding Numbers in D365 F&O x++

 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


AX 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);

}

This will populate finalResult with ABC-00001. If we use strLFix instead of strRFix, it will pad to the right with zeros, giving us ABC-10000.

Alternatively, you could use a function like this: (However, the approach above is most likely faster)

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

    return(out + item);
}

2 comments:

  1. You should take help from professionals who have immense experience on Dynamics 365 finance. They will help you with Solutions easily. Learn: Dynamics 365 finance and operation

    ReplyDelete
  2. Use strPadLeft(int2str(number), desiredLength, '0')webspacekit to apply zero padding to numbers in D365 F&O X++.






    ReplyDelete

Why do users often get confused about Dynamics 365 Customer Engagement (D365 CE), Dataverse, and the Power Platform?

  The Microsoft ecosystem for business applications can sometimes be difficult to navigate, especially when discussing Dynamics 365 Customer...