Friday, October 2, 2020

How to use strSplit() Function in x++

While doing customization I came across a scenario where the datatype of a column was of string type and value was stored as "1111.93799999999999994". And the requirement was to get  exact two digits after the decimal point. No rounding up/down was required. In the below code example you can achieve this.


class stringSplitFunctionDemo
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        str str1='1111.93799999999999994';
       
        List strlist=new List(Types::String);
        ListIterator iterator;
        str _Value,beforeDec,afterDec,finalOutput;
        int x;
        ;

        strlist=strSplit(str1,'.');
        iterator = new ListIterator(strlist);
        
        while(iterator.more())
        {
            x= x+1;

            if(x==1)
            {
                beforeDec =iterator.value();            
            }
            if(x==2)
            {
                afterDec = iterator.value();
            }
            iterator.next();
        }

        finalOutput = beforeDec +'.'+ subStr(afterDec,1,2);
        Info(finalOutput);


    }

}

No comments:

Post a Comment

Get Enum Id and Enum Value in D365FO using SQL and X++

 Below is the sql get enum id and enum value of a enum in D365 F&O using sql. SELECT   enumidtable . NAME           'Enum Name' ...