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
 
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
 
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.
 
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.
 
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.
 
None of the other functions worked properly for me (one rounded 3.333 up to 10) so I wrote my own.

If you just want to round up a decimal to the next integer, then this one works great and is very simple. Essentially truncate will always round down, so you add one to the result. Simple.

Code:
function RoundUp returns integer(dValue as decimal):

    return int(if dValue - truncate(dValue,0) = 0 then dValue else truncate(dValue,0) + 1).

end.

But if you'd like to round decimals up with precision, then this works beautifully. We're still essentially truncating plus one, but we are adding one to the last digit, so we drop the decimal down with truncate (3.333 > 3.33), turn it into a string, strip the decimal point (3.33 > 333), convert it to an integer, add one (333 > 334), then add the decimal back in (334 > 3.34).

Code:
/* FUNCTION RETURNS A DECIMAL ROUNDED UP TO THE PRECISION VALUE */
function RoundUp returns decimal(dValue as decimal,precision as integer):
    define variable newValue  as decimal   no-undo.
    define variable decLoc    as integer   no-undo.
    define variable tempValue as character no-undo.
    define var      tempInt   as integer   no-undo.
   
    /* IF THE TRUNCATED VALUE MATCHES THE INPUT VALUE, NO ROUNDING IS NECESSARY; RETURN THE ORIGINAL VALUE */
    if dValue - truncate(dValue,precision) = 0 then
        return dValue.
           
    /* IF THE ORIGINAL VALUE MINUS THE TRUNCATED VALUE LEAVES A REMAINDER THEN ROUND UP */
    else
    do:
        assign
            /* FINDS THE LOCATION OF THE DECIMAL SO IT CAN BE ADDED BACK IN LATER */
            decLoc    = index(string(truncate(dValue,precision)),".")
            /* TRUNCATES TO THE PRECISION POINT, DROPS THE DECIMAL, CONVERTS TO AN INT, THEN IF NEGATIVE SUBTRACTS ONE, IF POSITIVE ADDS ONE */
            tempValue = string(integer(replace(string(truncate(dValue,precision)),".","")) + if dValue < 0 then -1 else 1).
        /* ADDS THE DECIMAL BACK IN AT THE ORIGINAL LOCATION */
        assign
            substring(tempValue,(if decLoc = 0 then length(tempValue) + 1 else decLoc),0) = ".".
        /* RETURNS THE RESULTING VALUE AS A DECIMAL */
        return decimal(tempValue).
    end.
end.

Throw this into your scratchpad with the precision version above to see how it works!
Code:
message 
  "Round Up (Always Rounds Up)" skip
  roundup(-3.333333,4) skip
  roundup(-3.333333,3) skip
  roundup(-3.333333,2) skip
  roundup(-3.333333,1) skip
  roundup(-3.333333,0) skip
  "" skip
  "Round (Rounds Up or Down)" skip
  round(-3.333333,4) skip
  round(-3.333333,3) skip
  round(-3.333333,2) skip
  round(-3.333333,1) skip
  round(-3.333333,0) skip
  "" skip
  "Truncate (Always Rounds Down)" skip
  truncate(-3.333333,4) skip
  truncate(-3.333333,3) skip
  truncate(-3.333333,2) skip
  truncate(-3.333333,1) skip
  truncate(-3.333333,0) skip
  "" skip
  "Round Up (Always Rounds Up)" skip
  roundup(-3.666666,4) skip
  roundup(-3.666666,3) skip
  roundup(-3.666666,2) skip
  roundup(-3.666666,1) skip
  roundup(-3.666666,0) skip
  "" skip
  "Round (Rounds Up or Down)" skip
  round(-3.666666,4) skip
  round(-3.666666,3) skip
  round(-3.666666,2) skip
  round(-3.666666,1) skip
  round(-3.666666,0) skip
  "" skip
  "Truncate (Always Rounds Down)" skip
  truncate(-3.666666,4) skip
  truncate(-3.666666,3) skip
  truncate(-3.666666,2) skip
  truncate(-3.666666,1) skip
  truncate(-3.666666,0) skip
view-as alert-box.
message
  "Round Up (Always Rounds Up)" skip
  roundup(3.333333,4) skip
  roundup(3.333333,3) skip
  roundup(3.333333,2) skip
  roundup(3.333333,1) skip
  roundup(3.333333,0) skip
  "" skip
  "Round (Rounds Up or Down)" skip
  round(3.333333,4) skip
  round(3.333333,3) skip
  round(3.333333,2) skip
  round(3.333333,1) skip
  round(3.333333,0) skip
  "" skip
  "Truncate (Always Rounds Down)" skip
  truncate(3.333333,4) skip
  truncate(3.333333,3) skip
  truncate(3.333333,2) skip
  truncate(3.333333,1) skip
  truncate(3.333333,0) skip
  "" skip
  "Round Up (Always Rounds Up)" skip
  roundup(3.666666,4) skip
  roundup(3.666666,3) skip
  roundup(3.666666,2) skip
  roundup(3.666666,1) skip
  roundup(3.666666,0) skip
  "" skip
  "Round (Rounds Up or Down)" skip
  round(3.666666,4) skip
  round(3.666666,3) skip
  round(3.666666,2) skip
  round(3.666666,1) skip
  round(3.666666,0) skip
  "" skip
  "Truncate (Always Rounds Down)" skip
  truncate(3.666666,4) skip
  truncate(3.666666,3) skip
  truncate(3.666666,2) skip
  truncate(3.666666,1) skip
  truncate(3.666666,0) skip view-as alert-box.

Here are my results:
1745480229423.png
1745480247336.png
 
Last edited:
Back
Top