ILE RPG Ceiling()

This subprocedure implements a ceiling function: the smallest integer greater than or equal to value, given a scaling factor.

It is implemented to mimic Excel’s ceiling, in that the returned value is farthest from zero.


     d ceiling         pr            10i 0
     d  value                        15s 5 const
     d  factor                       15s 5 const

     p ceiling         b

     d ceiling         pi            10i 0
     d  value                        15s 5 const
     d  factor                       15s 5 const

     d numtemp         s             10i 0 inz
     d sign            s              1s 0 inz

      /free

         if factor = 0;
            return 0;
         else;
            sign = 1;
            if value < 0;
               sign = -1;
            endif;
            numtemp = %int(value/factor);
            if numtemp = (value/factor);
               return %int(value);
            else;
               return (%abs(numtemp)+1)*factor*sign;
            endif;
         endif;

      /end-free
     p ceiling         e