ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Coverting Char to Numeric

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

  • Coverting Char to Numeric

    I have a character field that will contain data such as "**********12.00".
    I need to move the numeric part of the field to a numeric field that is 11 positions with 2 decimals.
    For some reason today I don't even know where to start with this (it's been that kind of day). Anyone have a good idea that could get me started?

    Jane22

  • #2
    Re: Coverting Char to Numeric

    like this ??

    Code:
     *                                                             
     * Definitions                                                 
     *                                                             
    d somefield       s             15    inz('**********12.35')   
    d dec152          s             15  2                          
     *                                                             
     /free                                                         
                                                                   
          somefield =  %xlate('*':'0':somefield);                  
          dec152 = %dec(somefield:15:2);                           
                                                                   
           *INLR = *on;                                            
     /end-free

    -- or --

    Code:
    dec152  =  %dec(%xlate('*':'0':somefield):15:2);
    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


    • #3
      Re: Coverting Char to Numeric

      Thanks Jamie,

      I figured it out right after I posted this question.

      Jane22

      Comment


      • #4
        Re: Coverting Char to Numeric

        Code:
        ****************************************************************
              *  Description.. Convert Charcter to Numeric                   *
                    
              ****************************************************************
             D #CtoN           PR            30P 9                                      Character to Numeric
             D   PR_Char                     32    VALUE
              *//////////////////////////////////////////////////////////////*
              * (#CtoN) Character to Numeric                                 *
              *                                                              *
              * Use: #CtoN(Character_Representation_of_Number)               *
              *                                                              *
              *//////////////////////////////////////////////////////////////*
             P #CtoN           B                   EXPORT
              *--------------------------------------------------------------*
             D #CtoN           PI            30P 9
             D  Char                         32    VALUE
              *
             D                 DS
             D Char1                          1
             D Num1                           1  0 OVERLAY(Char1) INZ
              *
             D Num             S             30P 9
             D WrkNum          S             30P 0
             D Sign            S              1  0 INZ(1)
             D DecPos          S              3  0
             D Decimal         S              1    INZ('N')
             D i               S              4  0
             D j               S              4  0
              *--------------------------------------------------------------*
             C                   eval      Char = %triml(Char)
             C     ' '           CHECKR    Char          j                        99
              *
             C                   if        (not *IN99)
             C                   eval      j = %size(Char)
             C                   endif
              *
             C     1             do        j             i
             C                   eval      Char1 = %subst(Char:i:1)
              *
             C                   select
             C                   when      (Char1 = '-')
             C                   eval      Sign = -1
             C                   when      (Char1 = '.')
             C                   eval      Decimal = 'Y'
             C                   when      (Char1 >= '0') and (Char1 <= '9')
             C                   eval      WrkNum = (WrkNum * 10 + Num1)
              *
             C                   if        (Decimal = 'Y')
             C                   eval      DecPos = (DecPos + 1)
             C                   endif
              *
             C                   endsl
              *
             C                   enddo
              *
             C                   eval(h)   Num = (WrkNum * Sign / (10 ** DecPos))
             C                   RETURN    Num
              *--------------------------------------------------------------*
             C     *PSSR         BEGSR
             C                   RETURN    0
             C                   ENDSR
              *------------------------------------------

        Comment

        Working...
        X