Friday, June 08, 2007


RPGLE in FREE supports two ways to convert numerics to a character field. Both are Built In Functions, BIF. These are %Char and %EditC.

Both functions are replacements for the use of MOVE in RPG III/IV.

Let’s consider a positive numeric of 6 positions and 3 decimal places, numeric = 6.000.

myCharNum = %Char(numeric);

This produces a left adjust output of ‘6.000′.

myEditNum = %EditC(numeric: ‘X’);

This produces a right adjusted output of ‘006000′.

Notice the stripping of the decimal when using %EditC. Also to note is the addition of leading zeroes in the %EditC output.

Both options have their uses depending on what the text form of the number is for.

Let’s now consider a negative numeric, -6.000.

The %Char output is ‘-6.000′.

The %EditC ouput is ‘00600}’.

As you can see here, %EditC not only doesn’t maintain the integrity of the number, but provides nonnumeric output. Again, note that the %EditC function right adjusts the value.

From these tests it might appears that %Char might be your best and obvious choice for all numeric to character conversions. But there is more to %EditC than just the ‘X’ option. %EditC is actually short for edit code. It allows the use of all numeric edit codes for formatting. This includes most importantly the Date formats.

The proper edit code used in place of ‘X’, provides also for asterick fill in place of leading zeroes. Another possible edit choice adds a currency symbol.

In conclusion, for simple numeric conversions, %Char is probably the best and most straightforward choice. For more complex conversions, especially for dates, then %EditC is the only option.

In another entry I’ll a few of the other edit codes for %EditC.

No comments: