ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

slickest way to move packed decimal to char field

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

  • slickest way to move packed decimal to char field

    anything better than this?

    g_char = %subst(%editc(p_packed:'X'):1:xx) + '.' +
    %subst(%editc(p_packed:'X'):xx:xx);

  • #2
    It depends on what you want the result to look like. How about
    Code:
     g_char = %editc(p_packed:'J')
    changing the edit code to adjust how you want decimals, negatives, etc to appear.

    Comment


    • #3
      i think this is perfect - i'll give it a try - thank you

      Comment


      • #4
        what about the other way...? g_packed = g_char.... say g_packed is 6p5 and g_char is 1024

        Comment


        • #5
          As arrow says - it depends what you want. The absolute simplest would be g_char = %Char(p_packed);

          That gives you the decimal insertion and stripping of leading blanks. i.e 123.45 becomes '123.45' (6 characters) and 1.23 becomes '1.23' (3 characters).

          In addition to %EditC (which can use any edit code) you could also use %EditW which allows the use of any edit word.

          Comment


          • #6
            g_char = %TrimL(%Char(p_packed)) ;

            g_packed = %Dec(g_char:6:5) ;

            Comment


            • #7
              Originally posted by CRinger400 View Post
              g_char = %TrimL(%Char(p_packed)) ;

              g_packed = %Dec(g_char:6:5) ;
              i'm afraid, that will drop the decimal place and g_packed will become 1234

              Comment


              • #8
                Originally posted by jayvaughn View Post

                i'm afraid, that will drop the decimal place and g_packed will become 1234
                Are you sure? I thought that %char kept the decimal place?

                I do find however that %char strips all leading zero's, so 0.123 becomes ".123"

                Comment


                • #9
                  Originally posted by Vectorspace View Post

                  Are you sure? I thought that %char kept the decimal place?

                  I do find however that %char strips all leading zero's, so 0.123 becomes ".123"
                  yes just tested it... trying to avoid %subst, but unless anyone has a better idea...???

                  i was WRONG - went by the DSPLY of the field which left off the decimal point - put in debug and yes the new g_packed has the decimal - sorry!
                  Last edited by jayvaughn; August 2, 2017, 08:06 AM.

                  Comment


                  • #10
                    If you know the size of your packed decimal and want to keep at least 1 digit before the dp, then how about (for an 8p6):

                    %trim(%editw(num:'0 . -'));

                    Though annoyingly the -ve sign must be at the end with this method

                    Comment


                    • #11
                      while we are on the topic... does anyone know if host variables can be used place of the following for x... %dec(field:x:x);

                      I'm trying it and getting a compile error. but could be because they are integer data type...???

                      Comment


                      • #12
                        It is not possible to use variables as replacement of X:X!
                        What you can do you can replace the X with a constant, which is for example defined based on a reference field, i.e.
                        For Example:
                        Code:
                        DCL-C  ConstDecPos    Const(%DecPos(VarX));
                        DCL-C  ConstDecLen    Const(%Len(VarX));
                        
                        MyResult = %Dec(MyValue: ConstDecLen: ConstDecPos);
                        Birgitta

                        Comment


                        • #13
                          Originally posted by B.Hauser View Post
                          It is not possible to use variables as replacement of X:X!
                          What you can do you can replace the X with a constant, which is for example defined based on a reference field, i.e.
                          For Example:
                          Code:
                          DCL-C ConstDecPos Const(%DecPos(VarX));
                          DCL-C ConstDecLen Const(%Len(VarX));
                          
                          MyResult = %Dec(MyValue: ConstDecLen: ConstDecPos);
                          Birgitta
                          oh, now that is slicker than owl poop - thx Birgitta

                          Comment


                          • #14
                            If you are just assigning the result of %DEC to a packed field, you don't need to have the exact length and decimals of the packed field for the %DEC. If you use values that are larger than the length and decimals of the result, it will work fine. The only time this wouldn't work is if MyResult has more than 31 decimal positions.

                            Code:
                            MyResult = %Dec(MyValue: 63 : 31);

                            Comment


                            • #15
                              Originally posted by Barbara Morris View Post
                              If you are just assigning the result of %DEC to a packed field, you don't need to have the exact length and decimals of the packed field for the %DEC. If you use values that are larger than the length and decimals of the result, it will work fine. The only time this wouldn't work is if MyResult has more than 31 decimal positions.

                              Code:
                              MyResult = %Dec(MyValue: 63 : 31);
                              this is ideal!

                              Comment

                              Working...
                              X