3

I am writing a report for an MRP program and it contains a field I calculate for the quantity to order. I need to round the number up if it is a decimal point.

For example: 2.33 needs to be rounded up to 3 and so on.

I have tried

oder = round(order,0).

but that just get me 2.00 I need that number to be rounded up to the next whole number.

Jim S.
  • 203
  • 8
  • 24

4 Answers4

6
function roundUp returns integer ( x as decimal ):

  if x = truncate( x, 0 ) then
    return integer( x ).
   else
    return integer( truncate( x, 0 ) + 1 ).

end.

display roundUp( 2.33 ).
Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
2
x = round(n + 0.4999999999, 0) 

... should work for all for negative values of n too

  • n = -2.0000001 ... x = -2
  • n = -2.5 ... x = -2
  • n = -2.9999999999 ... x = -2

roundUp() resolves to -1 in all the above negative values of n

  • This only works when you are limited to 10 digits to the right of the decimal. – Tom Bascom Jan 06 '17 at 02:15
  • The proper behavior of rounding functions with negative arguments is a subject of some debate: http://math.stackexchange.com/questions/3448/rules-for-rounding-positive-and-negative-numbers – Tom Bascom Jan 09 '17 at 20:50
0

This will work for negative values too. Return value is intentionally kept as Integer, you can keep it as Decimal too.

function roundUp returns integer 
( 
    input in-value as DECIMAL 
):

    def var x as decimal no-undo.
    def var y as decimal no-undo.

    if in-value < 0 then
    DO:
        x = truncate(in-value,0).     
        y =  in-value - x.      
        if abs(y) <= 0.5 then 
            return INTEGER(x).
        else 
            return INTEGER(round(in-value,0)).
    END.
    ELSE 
        return INTEGER(round(in-value,0)).

end function.
-1

Use the following:

DEF VAR X AS decimal.
DEF VAR Y AS integer.

update
x with frame a.

Y = X - 1.
display y.
IF (( X - Y ) < 0.5)
THEN do:
DISPLAY round(x,0) + 1.
end.
ELSE DO :
DISPLAY round(x,0).
end.