Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NUMVAL 1

Status
Not open for further replies.

claudeb

Programmer
Nov 23, 2000
140
CA
Hi,
How do you use NUMVAL and what are its pittfalls ?
thanks
 
7.1.30 NUMVAL
Ÿ Copyright IBM Corp. 1991, 1998

The NUMVAL function returns the numeric value represented by the
alphanumeric character string specified in an argument. The function
strips away any leading or trailing blanks in the string, producing a
numeric value that can be used in an arithmetic expression.

The function type is numeric.

³--- Format -------------------------------------------------------------³
³ ³
³ >>--FUNCTION NUMVAL--(argument-1)----------------------------------->< ³
³ ³
³------------------------------------------------------------------------³
argument-1
must be a nonnumeric literal or an alphanumeric data item whose
content has either of the following formats:


³------------------------------------------------------------------------³
³ ³
³ >--³-------³--³---³--³-------³--³-digit--³--------------³-³----------> ³
³ ³-space-³ ³-+-³ ³-space-³ ³ ³-.--³-------³-³ ³ ³
³ ³---³ ³ ³-digit-³ ³ ³
³ ³-.--digit----------------³ ³
³ ³
³ >--³-------³---------------------------------------------------------> ³
³ ³-space-³ ³
³ ³
³------------------------------------------------------------------------³
³------------------------------------------------------------------------³
³ ³
³ >--³-------³--³-digit--³--------------³-³--³-------³--³----³---------> ³
³ ³-space-³ ³ ³-.--³-------³-³ ³ ³-space-³ ³-+--³ ³
³ ³ ³-digit-³ ³ ³----³ ³
³ ³-.--digit----------------³ ³-CR-³ ³
³ ³-DB-³ ³
³ ³
³ >--³-------³---------------------------------------------------------> ³
³ ³-space-³ ³
³ ³
³------------------------------------------------------------------------³
space
A string of one or more spaces.

digit
A string of one or more digits. The totals number of digits must not
exceed 18.


If the DECIMAL-POINT IS COMMA clause is specified in the SPECIAL-NAMES
paragraph, a comma must be used in argument-1 rather than a decimal point.

The returned value is an approximation of the numeric value represented by
argument-1.
7.1.31 NUMVAL-C
Ÿ Copyright IBM Corp. 1991, 1998

The NUMVAL-C function returns the numeric value represented by the
alphanumeric character string specified as argument-1. Any optional
currency sign specified by argument-2 and any optional commas preceding
the decimal point are stripped away, producing a numeric value that can be
used in an arithmetic expression.

The function type is numeric.

X The NUMVAL-C function cannot be used if any of the following are true:

X * The program contains more than one CURRENCY SIGN clause in the
X SPECIAL-NAMES paragraph of the Environment Division.

X * Literal-6 in the CURRENCY SIGN clause is a lowercase letter.

X * The PICTURE SYMBOL paragraph is specified in the CURRENCY SIGN clause.


³--- Format -------------------------------------------------------------³
³ ³
³ >>--FUNCTION NUMVAL-C--(argument-1-³------------³-)----------------->< ³
³ ³-argument-2-³ ³
³ ³
³------------------------------------------------------------------------³
argument-1
Must be a nonnumeric literal or an alphanumeric data item whose
content has either of the following formats:


³------------------------------------------------------------------------³
³ ³
³ >--³-------³--³---³--³-------³--³----³--³-------³--------------------> ³
³ ³-space-³ ³-+-³ ³-space-³ ³-cs-³ ³-space-³ ³
³ ³---³ ³
³ ³
³ >--³-digit--³--------------³--³--------------³-³--³-------³----------> ³
³ ³ ³ <----------³ ³ ³-.--³-------³-³ ³ ³-space-³ ³
³ ³ ³---,--digit-³-³ ³-digit-³ ³ ³
³ ³-.--digit----------------------------------³ ³
³ ³
³------------------------------------------------------------------------³
³------------------------------------------------------------------------³
³ ³
³ >--³-------³--³----³--³-------³--------------------------------------> ³
³ ³-space-³ ³-cs-³ ³-space-³ ³
³ ³
³ >--³-digit--³--------------³--³--------------³-³--³-------³----------> ³
³ ³ ³ <----------³ ³ ³-.--³-------³-³ ³ ³-space-³ ³
³ ³ ³---,--digit-³-³ ³-digit-³ ³ ³
³ ³-.--digit----------------------------------³ ³
³ ³
³ >--³----³--³-------³-------------------------------------------------> ³
³ ³-+--³ ³-space-³ ³
³ ³----³ ³
³ ³-CR-³ ³
³ ³-DB-³ ³
³ ³
³------------------------------------------------------------------------³
space
A string of one or more spaces.

cs
The string of one or more characters specified by argument-2. At
most, one copy of the characters specified by cs can occur in
argument-1.

digit
A string of one or more digits. The total number of digits must not
exceed 18.


If the DECIMAL-POINT IS COMMA clause is specified in the SPECIAL-NAMES
paragraph, the functions of the comma and decimal point in argument-1 are
reversed.


argument-2
If specified, must be a nonnumeric literal or alphanumeric data item,
subject to the following rules:

* argument-2 must not contain any of the digits 0 through 9, any
leading or trailing spaces, or any of the special characters + - .
,

* argument-2 can be of any length valid for an elementary or group
data item, including zero

* Matching of argument-2 is case-sensitive. For example, if you
specify argument-2 as 'Dm', it will not match 'DM', 'dm' or 'dM'.


If argument-2 is not specified, the character used for cs is the
currency symbol specified for the program.


The returned value is an approximation of the numeric value represented by
argument-1.
 
Code:
1.6.7.3 Converting to Numbers (NUMVAL, NUMVAL-C)                                   
Ÿ Copyright IBM Corp. 1991, 1998                                                   
                                                                                   
   The NUMVAL and NUMVAL-C functions convert character strings to numbers.         
   Use these functions to convert alphanumeric data items that contain free        
   format character representation numbers to numeric form and process them        
   numerically.  For example:                                                      
                                                                                   
        01  R            Pic x(20)  Value &quot;- 1234.5678&quot;.                           
        01  S            Pic x(20)  Value &quot;  $12,345.67CR&quot;.                        
        01  Total        Usage is Comp-1.                                          
            .                                                                      
            .                                                                      
            Compute Total = Function Numval(R) + Function Numval-C(S)              
                                                                                   
   The difference between NUMVAL and NUMVAL-C is that NUMVAL-C is used when        
   the argument includes a currency symbol and/or comma, as shown in the           
   example.  You can also place an algebraic sign in front or in the rear,         
   and it will be processed.  The arguments must not exceed 18 digits (not         
   including the editing symbols).  For exact syntax rules, see IBM COBOL          
   Language Reference.                                                             
                                                                                   
   Numeric Result:  Both NUMVAL and NUMVAL-C return long (double-precision)        
   floating-point values.  A reference to either of these functions,               
   therefore, represents a reference to a numeric data item.  For more             
   information on characteristics of numeric data, see Chapter 3, &quot;Numbers         
   and Arithmetic&quot; in topic 1.3.                                                   
                                                                                   
1.6.7.3.1 Why Use NUMVAL and NUMVAL-C                                              
Ÿ Copyright IBM Corp. 1991, 1998                                                   
                                                                                   
   When you use NUMVAL or NUMVAL-C you don't need to statically declare            
   numeric data in a fixed format and input data in a precise manner.  For         
   example, for this code:                                                         
                                                                                   
        01  X            Pic S999V99  leading sign is separate.                    
            .                                                                      
            .                                                                      
            Accept X from Console                                                  
                                                                                   
   The user of the application must enter the numbers exactly as defined by        
   the PICTURE clause.  For example:                                               
                                                                                   
        +001.23                                                                    
        -300.00                                                                    
                                                                                   
   However, using the NUMVAL function, you could code:                             
                                                                                   
        01  A            Pic x(10).                                                
        01  B            Pic S999V99.                                              
            .                                                                      
            .                                                                      
            Accept A from Console                                                  
            Compute B = Function Numval(A)                                         
                                                                                   
   and the input could be:                                                         
                                                                                   
         1.23                                                                      
         -300
 
I hope these clips out of the IBM manual are helpful.

Regards,

Crox
 
Hi Claude, all,

I think numval & numval-c are very powerful functions if you are sure that the input format follows the restrictions set forth in the manual. But if a terminal operator entered (quotes are mine) &quot; $$++12A1..aC&quot;, or any other combination of illegalities, your pgm will abend. This situation is probably a certainty in an on-line environment, and in my opinion, renders the function useless in this situation. Yes, you can pre-edit, but that puts us back where we started, writing complex edit routines.

You probably saw my post in the other forum; this is what I was getting at. I would have preferred IBM implementing it something like this:

Argument-1 is assumed to have the attributes quoted by Crox in his post. Any illegal characters will be xfered to arg-2 unchanged. The presence of repititions of any valid special chars will result in the leftmost (or rightmost) being xfered to arg-2; all other occurrances will be xfered to arg-2 as is.

ON OVERFLOW for the arith function being used could be invoked when the data xfered from arg-1 exceeds the capacity of arg-2.

Developing the sign in arg-2 (if it's required by the arg-2 pic) can be handled like this:
If the byte is numeric convert it to the proper sign; if not, xfere it as is.

Now a simple IF NUMERIC will tell you if the inputted field is truely numeric.

Now, I can't say I crossed all the Ts and dotted all the Is, but I think it's workable. With this approach arg-2 may have to be limited to DISPLAY and moved to another more esoteric field after the numeric test. The important thing is that the function should NEVER abend.

What do you think? Jack.
 
hello Slade,
to tell the truth, i didn't understand the answer to your question in the other forum.

i am having a hard time to send you an email. my listing is huge. i cut it into 7 seperate files, and it still being refused by the hotmail server. i'll find a trick.
thanks
 
Hi Claude,

I think I found your problem (why you can't find line# 3501). The listing you sent me was for module CSBM0201; your job abended in module CSRS0292. So, you have to look in the compile listing for module CSRS0292.

Regards, Jack

P.S. The attachments were not formatted properly. It may be a Word level mismatch or something. The next time you send, maybe you can try .TXT

 
hello Slade,
i checked the listing of csrs0294. same problem ! (with options LIST and NOOFF)
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top