RPGIV @ Work

A unique site for RPG and System i Lovers


Hi, this site will provide all what you need in System i and RPG developments.

My Name is Chamara Withanachchi, System i Expert and RPG Developer. And in the field for last 11 years.

I hope you will find lot of valuable information from this site

How to half-adjust in free format RPG Print E-mail
User Rating: / 0
Written by Chamara Withanachchi   

Two common methods for Half-Adjust in RPG free format:

1.) Half-adjust using Eval. This example converts US pounds to kilos:

  if pounds > 0;
   eval(h) kilos = pounds/2.204623;

2.) Half-adjust using %dech, note that this method requires length and number of decimals parameters and will not allow %len or %decpos as parms:

  if pounds > 0;
   kilos = %dech(pounds/2.204623: 11: 4);

According to IBM, half adjust adds 5 to the last or right-most decimal position +1. For example, if your result has four decimal positions half adjust will add 5 to the fifth decimal position which may or may not affect the fourth decimal position.

Half adjusting does have an effect in our example assuming the result field "kilos" has four decimal positions but the divisor in our calculation has six decimal positions and the result could have even more.

Without half-adjusting, to convert fourteen US pounds to kilos:
kilos = 14/2.204623 = 6.35029209075656011919...(well, you get the point)

With half-adjusting:
kilos = 6.3503

Other options are to use %UNSH (convert to unsigned format half-adjusting when only positive numbers are expected) and %INTH (convert to integer which basically just truncates any decimal places).

With %INTH:
kilos = 6.0000

<Previous   Next>