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

  • jayvaughn
    replied
    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!

    Leave a comment:


  • Barbara Morris
    replied
    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);

    Leave a comment:


  • jayvaughn
    replied
    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

    Leave a comment:


  • B.Hauser
    replied
    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

    Leave a comment:


  • jayvaughn
    replied
    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...???

    Leave a comment:


  • Vectorspace
    replied
    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

    Leave a comment:


  • jayvaughn
    replied
    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.

    Leave a comment:


  • Vectorspace
    replied
    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"

    Leave a comment:


  • jayvaughn
    replied
    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

    Leave a comment:


  • CRinger400
    replied
    g_char = %TrimL(%Char(p_packed)) ;

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

    Leave a comment:


  • JonBoy
    replied
    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.

    Leave a comment:


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

    Leave a comment:


  • jayvaughn
    replied
    i think this is perfect - i'll give it a try - thank you

    Leave a comment:


  • arrow483
    replied
    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.

    Leave a comment:


  • jayvaughn
    started a topic slickest way to move packed decimal to char field

    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);
Working...
X