
When working with AX (Dynamics AX), there are scenarios where padding numbers with zeros is necessary. One such example is in creating Positive Pay files, which often require fixed-width numeric values. Zero padding ensures that numbers maintain a consistent format, such as transforming “1” into “00001”. This guide demonstrates how to handle zero padding effectively in AX using built-in methods and custom functions.
The Use Case
AX provides straightforward ways to pad strings with additional characters. A common requirement is to simulate a “fake” number sequence, such as when incrementing a counter and displaying it in a specific format like:
- “ABC-00001”
- “ABC-00002”
This requires padding the counter with zeros to maintain the desired format.
Built-in Methods: strRFix and strLFix
AX includes two helpful methods for string padding:
strRFix: Pads characters to the left of the string.strLFix: Pads characters to the right of the string.
Here is an example:
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);
// Output: finalResult will be "ABC-00001"
}
strRFixpads zeros to the left of the numeric value.- If you use
strLFixinstead, it pads zeros to the right, resulting in a format like “ABC-10000”.
This approach is efficient and leverages AX’s built-in capabilities for string manipulation.
Custom Function for Zero Padding
If you prefer more control or flexibility, you can create a custom function to handle zero padding. Here is an example:
static str zeroPad(str item, int numZero)
{
int i;
str out;
;
for (i = 0; i < numZero - strlen(item); i++)
{
out += "0";
}
return (out + item);
}
How It Works:
- The function takes two parameters: the
itemto be padded and the number of zeros (numZero). - It calculates the difference between the desired length (
numZero) and the current string length. - It prepends the required number of zeros to the string and returns the result.
Performance Consideration
While the custom function provides flexibility, the strRFix method is typically faster and more efficient. For most use cases, leveraging AX’s native methods is recommended unless you need custom logic not supported by these methods.
Practical Example
Here’s how you can use both approaches in real-world scenarios like generating Positive Pay file numbers:
Using strRFix:
static void generatePositivePayNumber(Args _args)
{
int checkNumber = 123;
str paddedCheckNumber;
;
paddedCheckNumber = strRFix(int2str(checkNumber), 8, "0");
info(strFmt("Padded Check Number: %1", paddedCheckNumber));
// Output: "Padded Check Number: 00000123"
}
Using zeroPad Function:
static void generatePositivePayNumberCustom(Args _args)
{
int checkNumber = 123;
str paddedCheckNumber;
;
paddedCheckNumber = zeroPad(int2str(checkNumber), 8);
info(strFmt("Padded Check Number: %1", paddedCheckNumber));
// Output: "Padded Check Number: 00000123"
}
Conclusion
Zero padding numbers in AX is a common requirement for formatting data. By using built-in methods like strRFix and strLFix, or implementing a custom function, you can achieve this easily and efficiently. For most use cases, the native methods are sufficient and performant, while custom functions provide additional flexibility when needed.
For further reading, you can check out the original post that inspired this article
Leave a comment