ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Best Method

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

  • Best Method

    If I have a character parm coming into a program like this '0000000010.00' what is the best method of turning it into a numeric i.e. 10.00 ? I could create a data structure or array and take out the decimal but wanted to know what other people's thoughts on this.

    Thanks,

    DAC

  • #2
    Re: Best Method

    mynewdecimalfield = %dec(incharfield:7:2) ;
    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: Best Method

      If you wanted to be extra safe you could add a check also
      PHP Code:
      If %Check('0123456789.':incharfield)=0;
         
      mynewdecimalfield = %dec(incharfield:7:2) ;
      Else;
         
      // Handle error
      EndIf; 
      Ben

      Comment


      • #4
        Re: Best Method

        Or if you wanted to be even safer and check for even more errors than that, you could put the code inside a monitor group.
        Code:
             Monitor;
                mynewdecimalfield = = %dec(incharfield:7:2) ;
             On-Error;
                // Handle all errors
             EndMon;
        "Time passes, but sometimes it beats the <crap> out of you as it goes."

        Comment


        • #5
          Re: Best Method

          yes monitor is nice here is a small example.

          Code:
                d bigzero         s              5  0
               D dividebyzero    c                   00102
               d charfield       s              7
               d count           s              1  0
               d decimalfield    s              7  2
               d mystatus        s              5  0
          
                /free
          
                       for count = 1 to 3;
                        select;
                         when count =1;
                          charfield = '0001222';
                          monitor;
                           decimalfield = &#37;dec(charfield:7:2) ;
                          on-error;
                           mystatus = %status;
                           dsply %editc(mystatus:'X') ' ';
                          endmon;
                         when count =2;
                          charfield = '000ABCD';
                          monitor;
                          decimalfield = %dec(charfield:7:2) ;
                          on-error;
                           mystatus = %status;
                           dsply %editc(mystatus:'X') ' ';
                          endmon;
                         when count =3;
                          charfield = '12567';
                          monitor;
                          decimalfield = %dec(charfield:7:2) ;
                          decimalfield = decimalfield/bigzero;
                          on-error;
                           mystatus = %status;
                           dsply %editc(mystatus:'X') ' ';
                          endmon;
                        endsl;
                       endfor;
          
          
                    // monitor specifically for the devide by zeros code
          
                       monitor;
                       decimalfield = decimalfield/bigzero;
                       on-error dividebyzero;
                        decimalfield = *zeros;
                        dsply 'Hey! ShortBus you cant do that' ' ' ;
                       endmon;
          
                       *inlr = *on;
                /end-free
          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

          Working...
          X