Tip: Formatting Decimals

Today I had a classic encounter with the FORMAT command in C/AL.

One of my colleagues was busy generating an ASCII file from NAV in which he wanted to make sure a decimal value was formatted with a two digit precision. (ie. 1.1 should be formatted as 1.10). He asked if I knew a format string for it.

My first reaction was to try the next syntax.

FORMAT(MyDecimal, 0, '<Integer><Decimals,2>');

But the result of this code I got '1.1' as a result. At the same moment I found myself stepping into a classic programming error because NAV counts the decimal separator as one of the characters for the decimals! So by using the next code, you will get the right result (in this case: '1.10').

FORMAT(MyDecimal, 0, '<Integer><Decimals,3>');

 Since this is not the most intuitive thing to do, keep paying attention when formatting variables!

Comment List