Friday, January 23, 2015

Add trailing 0's to a number to display to a user. Ex: 12.78 VS 12.7800

So if you have dealt with displaying and transferring numbers that have trailing 0's within AX you will notice that all trailing 0's get dropped when you display the value to the user via code or try to use the variable in any way except tying the value/field directly to a control even though they get stored within the DB with trailing zeros.

For example if you display the following

real test1;

test1 = 2.3450000;

info(strFmt("%1", test1));




The user will be given/displayed a value of 2.345 instead if 2.3450000  most people wouldn't mind this but if you work somewhere that has to display the full length of a value tested then this no longer works.

The following method will take in any real value and apply either a default of 3 decimals or allow you to override that value with anything you specify



 /// <summary>
/// Add trailing zeros to a number if needed and return as string so we can display and pass it to other functions without 0's being droped
/// </summary>
/// <param name="numberToModify">
/// Number to add trailing 0's to
/// </param>
/// <param name="overrideDefaultDecimal">
/// Should we override the default decimal count of 3 and provide our own
/// </param>
/// <param name="decimalOveride">
/// Number of decimals to apply to number (override)
/// </param>
/// <returns>
/// String value with trailing 0's
/// </returns>
static str AddTrailingZeros(FWMDecimalToConvert numberToModify, boolean overrideDefaultDecimal = false,  int decimalOveride = 0)
{
    str formatType = "N";
    System.Double resultTemp;
    str formattedResult;
    int numberOfDecimals = 3;
   
    //check to see if we should override the default decimal precision
    if(overrideDefaultDecimal)
    {
        numberOfDecimals = decimalOveride;
    }
    //convert the rounded value to a double type so we can use outside classes to handle adding trailing zeros
    resultTemp = System.Convert::ToDouble(numberToModify);

    //convert the converted value back to string with trailing zero's
    formattedResult = resultTemp.ToString(formatType + int2str(numberofDecimals));
    return formattedResult;
}



So now if you call the following

real test1, test2;

test1 = 2.3450000;

test2 = 2.1;

info(strFmt("%1 vs %2", test1, ClassName::AddTrailingZeros(test2)));



You will get 2.35 vs 2.100

or you could call info(strFmt("%1 vs %2", test1, ClassName::AddTrailingZeros(test2, true, 7)));

You will get 2.35 vs 2.1000000

and you can then transfer that value anywhere without anything ever being dropped. The downside to this method is your variable(number) is now stored as a string instead of a real. I guess you cant have your cake and eat it to.

This method uses .net formatting functions to accomplish this so you could also change this function to format as percentages, dollars or whatever you like. This is defined via 'formatType = "N"'  You can check out the available format types at https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx