ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Round Up, Round Down and Half adjust

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Round Up, Round Down and Half adjust

    Does anyone know of a method, routine, procedure, bif, etc. to perform round up or round down (not to be confused with half adjust) . The chart below shoes the difference between round up, round down and half adjust:

    Click image for larger version

Name:	image.png
Views:	833
Size:	13.0 KB
ID:	158569

  • #2
    It has been about 25 years since I had to code this but something like this?
    Code:
    **free
    dcl-s rounded packed(5:2);
    dcl-s base packed(5:3) inz(12.999);
    dcl-s i int(5);
    for i = 1 to 100;
       eval(h) rounded = base + 0.004;
       dsply ( 'Base = ' + %char(base) + ' rounded = ' + %char(rounded) );
       base += 0.001;
    endfor;
    ​

    Comment


    • #3
      Use SQL:
      Code:
      With x (Number) as (Values(430,123), (430,125), (430,128))
      Select Number, Round(Number, 2)           "Round",
                     Ceiling(Number * 100) /100 "Round Up",
                     Floor(Number * 100) / 100  "Round Down"
        from X;​

      Comment


      • vazymimil
        vazymimil commented
        Editing a comment
        I think these in fact are round ceiling and round floor, and when input values are negative, round up is equivalent to Ceiling(abs(Number) * 100) /100 * Sign(Number)

    • #4
      Originally posted by B.Hauser View Post
      Use SQL:
      Code:
      With x (Number) as (Values(430,123), (430,125), (430,128))
      Select Number, Round(Number, 2) "Round",
      Ceiling(Number * 100) /100 "Round Up",
      Floor(Number * 100) / 100 "Round Down"
      from X;​
      That will only work if the the numbers to be rounded are integers, right? X has 3 decimal places in the examples I posted.

      Comment


      • vazymimil
        vazymimil commented
        Editing a comment
        I think these in fact are round ceiling and round floor, and when input values are negative, round up is equivalent to Ceiling(abs(Number) * 100) /100 * Sign(Number)

    • #5
      Rounding down is truncation. To round up compare the truncated value to the original value and adjust if needed.

      Code:
      dcl-ds;
        x zoned(5:3) pos(1);
        x_trunc zoned(5:2) pos(1);
      end-ds;
      
      dcl-s result zoned(5:2);
      
      
      // rounding down
      x = 430.123;
      result = x_trunc;    // result = 430.12
      
      x = 430.125;
      result = x_trunc;    // result = 430.12
      
      x = 430.128;
      result = x_trunc;    // result = 430.12
      
      
      // rounding up
      x = 430.123;
      if x > x_trunc;
        result = x_trunc + .01;    // result = 430.13
      endif;
      
      x = 430.125;
      if x > x_trunc;
        result = x_trunc + .01;    // result = 430.13
      endif;
      
      x = 430.128;
      if x > x_trunc;
        result = x_trunc + .01;    // result = 430.13
      endif;​

      Comment


      • UserName10
        UserName10 commented
        Editing a comment
        correction:

        x_trunc zoned(4:2) pos(1);

    • #6
      In RPG you could code like this:

      Code:
      dcl-pr ceil float(8) extproc(*cwiden:*dclcase);
                 val float(8) const;
      end-pr;​
      
      
      y=ceil( x *100) / 100;

      Comment


      • #7
        Originally posted by gregwga50 View Post

        That will only work if the the numbers to be rounded are integers, right? X has 3 decimal places in the examples I posted.
        If you had run my example you would have seen it is rounded (in all 3 cases) to 2 decimal positions.

        Comment


        • #8
          Originally posted by B.Hauser View Post

          If you had run my example you would have seen it is rounded (in all 3 cases) to 2 decimal positions.
          Sorry about that, you are usually always right on the money. I saw one of your posts from several years ago about rounding and made my conclusion from that, my bad.

          Comment

          Working...
          X