ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Convert a numeric field

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

  • Convert a numeric field

    Hi,
    I have a requirement to convert a 7s 2 field to a 7s 4 field. I know this is simple as defining a target field of 7s 4 and simply moving the source.
    The catch is, the target field size (precision and decimal positions) vary, and I want to write a service program to take the Source Value, Target Precision and Target decimals as input and convert the value.
    does anyone out there has a uutility/code to do this ?

    Thanks in advance.
    Sam

  • #2
    Re: Convert a numeric field

    Hi,

    just write a procedure which receives the numberic value and returns it. If you return it as return value and receive it into a numeric variable in any numeric format, the conversion should happen automatically. (It is not even necessary to pass the precision and decimal positions of the receiver variable.)

    Just give it a try
    PHP Code:
    P CvtNum           B

    D CvtNum           PI                         31P 9
    D  ParNum                                     31P 9    
    Const

     /
    Free
        
    Return ParNum;
     /
    EndFree
    P CvtNum           E 
    Birgitta

    Comment


    • #3
      Re: Convert a numeric field

      Hi thanks for the post.
      But the issue is that I have to accept the return value to a Alpha field. That way I have to define numeric variables for all possible combinations and then move that value to the Alpha field.
      I was looking to return the value converted to the specified size from the SP.

      Comment


      • #4
        Re: Convert a numeric field

        Hi rpgdude,

        Try the following (pseudo code, not tested, just wanted to point you in the hopefully right direction)

        myReturnValue = %char(%dec(myPassedInValue:myParmLength:myParmDec) )

        Where myParmLength and myParmDec are the length and decimal places you require

        Comment


        • #5
          Re: Convert a numeric field

          I tried it earlier, but program won't compile because it says that the 2nd and third parm for %DEC is invalid :

          PHP Code:
          dnum1             s             12    inz('123.43')             
          dchar1            s             12                              
          dp                s              3i 0 inz
          (4)                    
          dd                s              3i 0 inz(2)                    
           /
          free                                                          
               char1 
          = %char(%dec(num1p:d));                             
               
          dsply char1;                                               
               *
          inlr = *on
          Last edited by jamief; August 26, 2009, 12:20 PM. Reason: needed space between the ":" and "P" to not get :p

          Comment


          • #6
            Re: Convert a numeric field

            Now why did my : get replaced with a funny face lol

            Comment


            • #7
              Re: Convert a numeric field

              Hi,

              PHP Code:
              myReturnValue = %char(%dec(myPassedInValue:myParmLength:myParmDec) ) 
              Unfortunately this solution won't work, because the length and decimal postions must be known at compile time, i.e. they cannot be used dynamically.

              I recently run into the same problem, may be even worse. I get a pointer and the filename and field name of the original field. Based on this information I have to determine the data type, length and decimal position and retrieve the data from the pointer.

              (... and everything because my manager is convinced an RPG programmer is not able to remember 6 function names!)

              Birgitta

              Comment


              • #8
                Re: Convert a numeric field

                You are right Birgitta.

                Comment


                • #9
                  Re: Convert a numeric field

                  I found a solution thru a Java class, I will post the code when I get it working.

                  Comment


                  • #10
                    Re: Convert a numeric field

                    that's a darn shame that you can't define those dynamically...RPG frustrates me to no end some times...

                    Are you zero filling or truncating the value depending on what is passed in? Do you have to round up/down?

                    If not, maybe try playing around with %editc(value:'X') and %subst? Just turn your value into a character right from the get go and manipulate it that way? Just an idea...

                    Comment


                    • #11
                      Re: Convert a numeric field

                      If you want to return a character string, then you could modify Birgitta's code to something like this: (unless I am missing the point somewhere)

                      Code:
                           p ConvertNumber   b                           
                            
                           d ConvertNumber   pi            32a   Varying
                           d    Number                     31p 9 Const
                      
                           d  NumberString   s             32a   Varying
                      
                            /Free
                            
                               
                               NumberString = %char( NUMBER );
                            
                               dow %subst( NumberString : %len( NumberString ) : 1 ) = '0';
                                 NumberString = %subst( NumberString : 1 : %len( NumberString ) -1);
                               enddo;
                      
                               return NumberString;
                            
                            /end-free
                           p                 e
                      Michael Catalani
                      IS Director, eCommerce & Web Development
                      Acceptance Insurance Corporation
                      www.AcceptanceInsurance.com
                      www.ProvatoSys.com

                      Comment


                      • #12
                        Re: Convert a numeric field

                        hey I can do that in wayyyyyy more code~!


                        this only works one way........ smaller to bigger
                        ...oh yea... its needs a re-write....


                        Code:
                             d Indec           s           1000    inz('15,741.23')
                             d IntoDec         s             15  5 inz(4)
                        
                             d count           s              4  0
                             d found           s              4  0
                        
                             d Numeric         s              4  0
                             d Decimal         s              4  0
                        
                             d foundcomma      s               n
                        
                             d OutDec          s           1000
                              /free
                        
                                 // remove the ',' if passed only need "."
                        
                                 found = %scan (',': InDec);
                                 Dow found > *zeros;
                                  foundcomma = *on;
                                  Indec = %replace ('': InDec :  %scan (',': InDec): 1 );
                                  found = %scan (',': InDec);
                                 enddo;
                        
                                 Numeric = %len(%trim(Indec));
                                 Decimal =  %len(%subst(Indec:%scan('.':Indec:1)+1:
                                            Numeric - %scan('.':Indec:1)));
                        
                                  // remove "."
                                  Indec = %replace ('': InDec :  %scan ('.': InDec): 1 );
                        
                                 // now I know total length of decimal as well as decimal places(Decimal)
                        
                                 OutDec =  %subst(Indec:1:(Numeric-Decimal-1));
                        
                                 OutDec = %trim(OutDec) + '.';
                                
                                 OutDec = %trim(OutDec) +
                                          %subst(Indec:Numeric-decimal:decimal);
                        
                                  // add *zeros
                                 for count = 1 to Decimal;
                                  OutDec = %trim(OutDec) + '0';
                                 endfor;
                        
                        
                                 if foundcomma = *on;    // put commas back
                                 endif;
                        
                        
                                 *inlr = *on;
                        
                              /end-free
                        Attached Files
                        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


                        • #13
                          Re: Convert a numeric field

                          Umm why exactly is this conversion being done? Considering that youre changing a field from 7S 2 to 7S 4, you do end up losing data.

                          Example: 12345.67 would be converted as ? (maybe 345.67 or 123.45 or 999.99 depending upon the conversion you do)

                          If you wish to retain the previous value and simply increase the precision, you would need to go for 9S 4 in order to keep the mantissa part correct.

                          Example: 12345.67 would be converted as 12345.6700

                          Comment


                          • #14
                            Re: Convert a numeric field

                            If you have to return a character string with arbitrary precision then CVTNC might do it for you - I know I have an example somewhere but cannot locate it - sorry.

                            You really need to go the API or prototyped MI function for this kind of stuff.


                            Jon P.

                            Comment


                            • #15
                              Re: Convert a numeric field

                              Hi,

                              if you only want to convert a numeric value into a character value with a given length and number of decimal positions, you may also use embedded SQL.
                              That means build your SQL Statement, that converts a numeric value into a character representation dynamically and return the character value.

                              I just created a quick and dirty small procedure (without any error handling):
                              PHP Code:
                               *************************************************************
                              P CvtNumToChar    B                                                     
                              D CvtNumToChar    PI            50A   Varying                           
                              D  ParNum                       31P 9 
                              Const                             
                              D  ParLen                        3U 0 Const                             
                              D  ParDecPos                     3U 0 Const                             
                                                                                                      
                              D String          S           1024A   Varying                           
                              D RtnChar         S             50A   Varying                           
                               
                              *----------------------------------------------------------------------
                               /
                              Free                                                                  
                                  String 
                              'Values(Select Cast(Dec(' + %Char(ParNum) + ', ' +         
                                                                       %
                              char(ParLen) + ', ' +         
                                                                       %
                              Char(ParDecPos) + ') +        
                                                               as VarChar(50)) +                      
                                                     From SysIbm/SysDummy1) +                         
                                            into ?'
                              ;                                                  
                                  
                              Exec SQL  Prepare DynSQL from :String;                
                                  
                              Exec SQL  Execute DynSQL using :RtnChar;                            
                                  Return 
                              RtnChar;            
                               /
                              End-Free                     
                              P CvtNumToChar    E 
                              This procedure can be called as follows:

                              PHP Code:
                               /Free 
                                 MyChar 
                              CvtNumToChar(123450);                 
                                 
                              Dsply MyChar;                                         
                                 
                              MyChar CvtNumToChar(111,22394);
                                 
                              Dsply MyChar;                                         
                                 
                              MyChar CvtNumToChar(-111222333,44151);
                                 
                              Dsply MyChar;
                                 
                              MyChar CvtNumToChar(MyNumFldMyLenMyDecPos);
                                 
                              Dsply MyChar;
                               /
                              End-Free 
                              Birgitta
                              Last edited by B.Hauser; August 26, 2009, 11:55 PM.

                              Comment

                              Working...
                              X