Tuesday 9 November 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);
}

1 comment:

  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

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...