Question Round Up

A basic question guys..

I know how to use round in decimal but in my case is i need to round up an integer whenever it ends in 1.

Ex.

191 to 200
1124 to 1230
1242 to 1250

something like this..

Any reply would be so much appreciated. :) thanks
 

Cringer

ProgressTalk.com Moderator
Staff member
There is no built in function to do this so you'll have to roll your own.
Code:
function roundup returns integer
    (input iint as integer,
    input precision as integer) forward.

define variable myint as integer no-undo.

myint = 1124.

message roundup(myint,1) view-as alert-box.

function roundup returns integer
    ( input iint as integer,
    input precision as integer ):

    define variable multiplier as integer no-undo.
    define variable result     as integer no-undo.

    multiplier = exp(10,precision).

    if iint mod multiplier eq 0 then
        result = iint.
    else result = iint + multiplier - (iint mod multiplier).

    return result.

end function.
 
Thanks Mr. Cringer,

But I am not using this 'function' thing, i am a beginner, I tried your code in a procedure editor and it works.

now the problem is, I want to work this in a decimal.

eg.
1010.01 to 1020.00
1120.04 to 1130.00
 

Miguel Angel

New Member
Try this

DEF VAR val AS INT.
UPDATE val.
RUN roundup (INPUT-OUTPUT val).
MESSAGE val.
PROCEDURE roundup :
DEFINE INPUT-OUTPUT PARAMETER num AS INT.
num = ( TRUNCATE(num / 10 , 0) + 1 ) * 10.
END.
 

TomBascom

Curmudgeon
Regarding "try this"... That code does not work.

Questions about how to implement rounding functions are actually pretty good interview questions. I am constantly amazed at programmers who cannot do this stuff properly. It is a very quick way to weed out unqualified people.
 

Fabio

New Member
This is mine version.

It works like should be, i.e., negative numbers, multiples of 10 and decimal values.

Code:
FUNCTION GetRoundedUp RETURNS INT(deValue AS DEC):

  DEF VAR iResult   AS INT NO-UNDO.
  DEF VAR iMod      AS INT NO-UNDO.
  DEF VAR deRounded AS DEC NO-UNDO.

  ASSIGN deRounded = ROUND(ABS(deValue), 0)
              iMod = deRounded MOD 10.

  IF iMod = 0 AND (deRounded - ABS(deValue)) = 0 THEN
    iResult = deRounded.
  ELSE
    iResult = deRounded + 10 - iMod.

  RETURN IF deValue < 0 THEN - iResult ELSE iResult.

END.

MESSAGE
  GetRoundedUp(0)       SKIP
  GetRoundedUp(10)      SKIP
  GetRoundedUp(50)      SKIP
  GetRoundedUp(190)     SKIP
  GetRoundedUp(191)     SKIP
  GetRoundedUp(1124)    SKIP
  GetRoundedUp(1242)    SKIP
  GetRoundedUp(1010.01) SKIP
  GetRoundedUp(1010.49) SKIP
  GetRoundedUp(1010.51) SKIP
  GetRoundedUp(1010.99) SKIP
  GetRoundedUp(1120.04) SKIP
  GetRoundedUp(-190)    SKIP
  GetRoundedUp(-191)    SKIP
  VIEW-AS ALERT-BOX.
 
Top