ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Round up to next integer

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

  • Round up to next integer

    I have a situation in which I always need to round a decimal up to the next integer. How would I accomplish this? Eval (h) won't do it, and I was just wondering if there was anything I could do short of writing my own logic that would accomplish this? A biff? Or opcode? or extender?
    Your future President
    Bryce

    ---------------------------------------------
    http://www.bravobryce.com

  • #2
    Re: Round up to next integer

    %inth or %dech

    Code:
    HowmanyB = %inth(((TotalB * (364/Bdays)/50) * workweeks));      
    if %dech(((TotalB * (364/Bdays)/50) * workweeks):15:5) > *zeros 
       and                                                          
       %dech(((TotalB * (364/Bdays)/50) * workweeks):15:5) < 1;     
     HowmanyB += 1;                                                 
    endif;
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: Round up to next integer

      *Old School Alert*

      You can always add .5 to the number and take the truncated value as well.

      -R

      Comment


      • #4
        Re: Round up to next integer

        No dice Jamie, I need it to ALWAYS round up. For example...

        123.789 needs to round to 124
        123.234 needs to round to 124 as well.

        I think my only solution might be to do something like this....
        Code:
        myrem = %rem(field1:field2);
        if myrem <> 0;
          mydecfld = field1/field2;
          myintfld = mydecfld + 1;
        endif;
        I'm not sure of any shorter way to do it... half adjusting just isn't what I need.
        Your future President
        Bryce

        ---------------------------------------------
        http://www.bravobryce.com

        Comment


        • #5
          Re: Round up to next integer

          bryce thats what my code above does..

          jamie
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment


          • #6
            Re: Round up to next integer

            I think mine is easier to read

            and I think I could probably put it into something like this even...

            Code:
            if %rem(field1:field2) <> 0;
              myintfld = (field1/field2) + 1;
            endif;
            Your future President
            Bryce

            ---------------------------------------------
            http://www.bravobryce.com

            Comment


            • #7
              Re: Round up to next integer

              Just hit a snag with %rem. It turns out that field1 and field2 in the above example can only be integers. what a crock of shizza....

              Let me take a further look at your example Jamie.
              Your future President
              Bryce

              ---------------------------------------------
              http://www.bravobryce.com

              Comment


              • #8
                Re: Round up to next integer

                yes just use mine as an example....did you notice that I can change the size of the dec field to 15,5 or whatever on the fly.
                All my answers were extracted from the "Big Dummy's Guide to the As400"
                and I take no responsibility for any of them.

                www.code400.com

                Comment


                • #9
                  Re: Round up to next integer

                  Hi,

                  there is an SQL scalar function CEILING (or CEIL) that always rounds up to the next integer.

                  PHP Code:
                  C/Exec SQL   Set :Rounded Ceiling(:OrigValue)
                  C/End-Exec 
                  Birgitta

                  Comment


                  • #10
                    Re: Round up to next integer

                    Something isn't working there Jamie.

                    When I'm running it in debug mode and I do an F11 on the %dech() calls it tells me that a Syntax error occurred and won't give me the value. So it never goes into the if statement. What could be wrong? Do you have to store the value in a variable and then use that in the logic statement?
                    Your future President
                    Bryce

                    ---------------------------------------------
                    http://www.bravobryce.com

                    Comment


                    • #11
                      Re: Round up to next integer

                      aaahhhh, Thank you Birgitta. I believe you have given me my answer

                      I'd still like to find an RPG solution, just in case I need it sometime.
                      Your future President
                      Bryce

                      ---------------------------------------------
                      http://www.bravobryce.com

                      Comment


                      • #12
                        Re: Round up to next integer

                        Bryce:

                        Add .9999 to your calc. FastOne's solution will half round .9999 will round up (use as many 9s as you decimal position will go out to)

                        Best of Luck
                        GLS
                        The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

                        Comment


                        • #13
                          Re: Round up to next integer

                          I forgot bout that Birgitta thanks for the reminder....@#%@#%@#%@#% Im old
                          jamie
                          All my answers were extracted from the "Big Dummy's Guide to the As400"
                          and I take no responsibility for any of them.

                          www.code400.com

                          Comment


                          • #14
                            Re: Round up to next integer

                            Originally posted by bryce4president View Post
                            I'd still like to find an RPG solution, just in case I need it sometime.
                            Your situation may need additional decimal places, but here's one option.
                            Code:
                                 D                 DS
                                 D myNumber                      15S 5
                                 D   myInteger                   10S 0 Overlay(myNumber)
                                 D   myDecimal                    5S 5 Overlay(myNumber : *Next)
                            
                                  /free
                                      myNumber = (field1 / field2);
                                      If (myDecimal > *Zeros);
                                         myInteger += 1;
                                      EndIf;
                            http://www.linkedin.com/in/chippermiller

                            Comment


                            • #15
                              Re: Round up to next integer

                              how about just
                              Eval(H) Integer = Decimal + .49

                              Comment

                              Working...
                              X