ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Leading Zero's

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

  • Leading Zero's

    Hi,
    There is character field of length 3 and it has a value of '12', and there is another variable is of length '10'. I need the value which is there in the first field which is of length 3 is to be moved to the variable of length 10 and the when i display the variable it should show '0000000012'

    Thanks.

  • #2
    Re: Leading Zero's

    If the value is always numeric:

    var2 = %char(%dec(var1:10:0))

    Comment


    • #3
      Re: Leading Zero's

      If the values are character one of these should work

      Code:
      d char10          S             10                                   
      d char3           S              3    inz('12')                      
      c                   evalr       char10=('000000000' + %trim(char3))                 
       /free                                                               
        %subst(char10: 11-%len(%trim(char3)) : %len(%trim(char3))) =       
           %Trim(char3);                                                                                             
        *inlr=*on;                                                         
       /end-free
      Best of luck
      GLS
      Last edited by GLS400; December 20, 2006, 03:04 PM. Reason: lost a ( when posting
      The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

      Comment


      • #4
        Re: Leading Zero's

        Vinod,

        You can also use:

        Code:
        CharVar = %EDITC( NumVar : 'X' );
        HTH,
        MdnghtPgmr
        "Tis better to be thought a fool then to open one's mouth and remove all doubt." - Benjamin Franklin

        Comment


        • #5
          Re: Leading Zero's

          MdnghtPgmr is right. Use %editC instead of %char. So the complete statement becomes
          var2 = %editC(%dec(var1:10:0):'X')

          Note that this only works if you are dealing with numbers & blanks. If you have letters mixed in, use GLS400's method

          Comment


          • #6
            Re: Leading Zero's

            Another way if the target is character:


            Code:
            D CHAR3           S              3A   INZ(' 12') 
            D CHAR10          S             10A              
             /FREE                                           
                EVALR CHAR10 = %TRIM(CHAR3);                        
                CHAR10 = %XLATE(' ':'0':CHAR10);             
                RETURN;                                      
             /END-FREE
            Goodbye

            Comment

            Working...
            X