ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Conversion from String to Zoned decimal data type.

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

  • Conversion from String to Zoned decimal data type.

    Hi,


    Can anyone tell me how can we convert a String data (I mean, Character data) into Zoned decimal data?


    Thanks,
    Subha.

  • #2
    Re: Conversion from String to Zoned decimal data type.

    Try something like this.



    D cust5 s 5a inz('01120')
    D cust70 s 7s 0

    /free

    cust70 = %INT(cust5);

    Comment


    • #3
      Re: Conversion from String to Zoned decimal data type.

      slabdie answer is correct if you don't need decimals. If you have decimal positions then use the %dec


      Examples from IBM manual V5R3

      Using %Int
      PHP Code:
      D p7              s              7p 3 inz (1234.567)
      D s9              s              9s 5 inz (73.73442)
      D f8              s              8f   inz (123.789)
      D c15a            s             15a   inz (' 12345.6789 -'
      D c15b            s             15a   inz (' + 9 8 7 . 6 5 4 '
      D result1         s             15p 5
      D result2         s             15p 5
      D result3         s             15p 5
      array           s              1a   dim (200)
      D a               s              1a
       
       
      /FREE
       
      // using numeric parameters 
          
      result1 = %int (p7) + 0.011;  // "result1" is now 1234.01100.
          
      result2 = %int (s9);          // "result2" is now   73.00000
          
      result3 = %inth (f8);         // "result3" is now  124.00000.
       // using character parameters 
          
      result1 = %int (c15a);        // "result1" is now  -12345.00000 
          
      result2 = %inth (c15b);       // "result2" is now     988.00000 
           // %INT and %INTH can be used as array indexes
          
      = array (%inth (f8));
       /
      END-FREE 
      Using %dec
      PHP Code:
      D num1            S              7P 2
      D NUM1_LEN        C                   
      %len(num1)
      D NUM1_DECPOS     C                   %decpos(num1)    
      D num2            S              5S 1
      D num3            S              5I 0 inz
      (2)
      D chr1            S             10A   inz('Toronto   ')
      D chr2            S             10A   inz('Munich    ')
      D ptr             S               *
       
       * 
      Numeric expressions:
       /
      FREE
         num1 
      = %len(num1);                 //  7
         
      num1 = %decpos(num2);              //  1
         
      num1 = %len(num1*num2);            // 12
         
      num1 = %decpos(num1*num2);         //  3
         // Character expressions:
         
      num1 = %len(chr1);                 // 10
         
      num1 = %len(chr1+chr2);            // 20
         
      num1 = %len(%trim(chr1));          //  7
         
      num1 = %len(%subst(chr1:1:num3) + ' ' + %trim(chr2));//  9
         // %len and %decpos can be useful with other built-in functions:
         // Although this division is performed in float, the result is
         // converted to the same precision as the result of the eval:
         // Note: %LEN and %DECPOS cannot be used directly with %DEC
         //       and %DECH, but they can be used as named constants 
         
      num1 27 + %dec (%float(num1)/num3 NUM1_LEN NUM1_DECPOS);
         
      // Allocate sufficient space to hold the result of the catenation
         // (plus an extra byte for a trailing null character):
         
      num3 = %len (chr1 chr2) + 1;
         
      ptr = %alloc (num3);
         %
      str (ptrnum3) = chr1 chr2;
       /
      END-FREE 
      Never trust a dog to watch your food.

      Comment

      Working...
      X