Two common methods for Half-Adjust in RPG free format:
1.) Half-adjust using Eval. This example converts US pounds to kilos:
/free
kilos=0;
if pounds > 0;
eval(h) kilos = pounds/2.204623;
endif;
/end-free
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:
/free
kilos=0;
if pounds > 0;
kilos = %dech(pounds/2.204623: 11: 4);
endif;
/end-free
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
Free Format SQLRPGLE Shell Program: part 3
Author: Lionel Halvorsen
Mar 1, 2010
To carry the shell example a little further, one could use the DSPOBJD command to create an outfile for the entire library and then run this program.
The program does a descending sort by object size and then prints a total at the bottom. This program could be easily modified to check library and/or object sizes.
d total s 13p 0 inz
d count s 9p 0 inz
*
d filestr ds
d odlbnm 10a
d odobnm 10a
d odobtp 8a
d odobat 10a
d odobtx 50a
d odobsz 10p 0
*
/free
// write column headers
exsr setup;
// loop through file
exec sql
declare C1 cursor for
SELECT odlbnm,odobnm,odobtp,odobat,odobtx,odobsz
FROM qgpl/dspobjd
ORDER BY odobsz desc
for fetch only;
exec sql OPEN C1;
dow sqlcod=0;
exec sql FETCH C1 INTO:filestr;
if sqlcod=0;
exsr movdet;
endif;
enddo;
exec sql CLOSE C1;
except tot1;
*inlr=*on;
return;
begsr movdet;
exsr overflowSR;
except det1;
total+=odobsz;
count+=1;
endsr;
begsr setup;
except head1;
endsr;
begsr overflowSR;
if OverFlow;
except head1;
endif;
endsr;
/end-free
oqsysprt e head1 2 1
o 14 'Size'
o 29 'Library'
o 40 'Object'
o 51 'Type'
o 63 'Attribute '
o 75 'Description'
o e det1 1
o odobsz j 15
o odlbnm 29
o odobnm 40
o odobtp 50
o odobat 63
o odobtx 114
o e tot1 1
o 16 'Library Total='
o total j 34
o 50 'Object Count='
o count j 65
Mar 1, 2010
To carry the shell example a little further, one could use the DSPOBJD command to create an outfile for the entire library and then run this program.
The program does a descending sort by object size and then prints a total at the bottom. This program could be easily modified to check library and/or object sizes.
*-------------------------------------
* library size
*-------------------------------------
fqsysprt o f 132 printer oflind(OverFlow) d total s 13p 0 inz
d count s 9p 0 inz
*
d filestr ds
d odlbnm 10a
d odobnm 10a
d odobtp 8a
d odobat 10a
d odobtx 50a
d odobsz 10p 0
*
/free
// write column headers
exsr setup;
// loop through file
exec sql
declare C1 cursor for
SELECT odlbnm,odobnm,odobtp,odobat,odobtx,odobsz
FROM qgpl/dspobjd
ORDER BY odobsz desc
for fetch only;
exec sql OPEN C1;
dow sqlcod=0;
exec sql FETCH C1 INTO:filestr;
if sqlcod=0;
exsr movdet;
endif;
enddo;
exec sql CLOSE C1;
except tot1;
*inlr=*on;
return;
begsr movdet;
exsr overflowSR;
except det1;
total+=odobsz;
count+=1;
endsr;
begsr setup;
except head1;
endsr;
begsr overflowSR;
if OverFlow;
except head1;
endif;
endsr;
/end-free
oqsysprt e head1 2 1
o 14 'Size'
o 29 'Library'
o 40 'Object'
o 51 'Type'
o 63 'Attribute '
o 75 'Description'
o e det1 1
o odobsz j 15
o odlbnm 29
o odobnm 40
o odobtp 50
o odobat 63
o odobtx 114
o e tot1 1
o 16 'Library Total='
o total j 34
o 50 'Object Count='
o count j 65

