Page 1 of 1

Convert Char to Numeric

PostPosted: Thu Jun 17, 2010 7:46 am
by pbc3199
Dear all,
I'm Easytrieve beginner. COuld you please show mw the sample coding how to convert char to numeric?

Re: Convert Char to Numeric

PostPosted: Thu Jun 17, 2010 8:10 am
by dick scherrer
Hello and welcome to the forum,

Post the cobol picture of the input and the output fields you want to convert. Show some sample input values.

Re: Convert Char to Numeric

PostPosted: Thu Jun 17, 2010 1:54 pm
by pbc3199
My input file have WS-SERIAL 5 N.
I want to set to WS-SERIAL-NO 5 A

Re: Convert Char to Numeric

PostPosted: Fri Jun 18, 2010 2:13 am
by dick scherrer
Hello,

You will do better in the future if you provide what is requested rather than something else you choose to post. . . This time - no problem, but often there are delays when people do not provide requested info.

This:
DEFINE WS-SERIAL    W 5 N VALUE 234   
DEFINE WS-SERIAL-NO W 5 A         
WS-SERIAL-NO = WS-SERIAL                       
DISPLAY 'WS-SERIAL-NO AFTER NOVE ' WS-SERIAL-NO


Gives:
WS-SERIAL-NO AFTER NOVE 00234

Re: Convert Char to Numeric

PostPosted: Fri Oct 01, 2010 3:11 pm
by NicC
raj1984 wrote:It's a very simple program.

First, determine the amount of numbers entered. Then convert each char number into an integer. So, let's say you have 3 numbers - 1, 2, and 3. Declare an integer multiplier variable and set it equal to 1. Declare a float sum and set it equal to 0. Then do this algorithm:

Multiply the last number by multiplier and add it to a sum.
Multiply the multiplier by 10.
Repeat with the next number.

So with 1, 2, and 3 the algorithm will look like - 3 + 20 + 100, which in the end will give you the float sum that equals to 123.


Why do all that when a simple assignment will do as shown by Dick?

Re: Convert Char to Numeric

PostPosted: Fri Oct 01, 2010 3:11 pm
by NicC
Oh - I see. You are spamming.

Re: Convert Char to Numeric

PostPosted: Fri Oct 01, 2010 8:56 pm
by kramana9999
use function numval(char dataitem)

Re: Convert Char to Numeric

PostPosted: Fri Oct 01, 2010 11:54 pm
by dick scherrer
Hello and welcome to the forum,

use function numval(char dataitem)
Have you tested this in Easytrieve to be sure it works. . .

Please do not post untested "solutions".

Re: Convert Char to Numeric

PostPosted: Fri Oct 15, 2010 11:28 am
by Scott Lippincott
In Dick's code sample there is no difference in the contents of WS-SERIAL and WS-SERIAL-NO after the assignment. The difference in the way they are displayed is due to the application of a default edit mask.
DEFINE WS-SERIAL W 5 N VALUE 234
DEFINE WS-SERIAL-NO W 5 A
WS-SERIAL-NO = WS-SERIAL
DISPLAY 'WS-SERIAL-NO AFTER NOVE ' WS-SERIAL-NO
In fact, the two fields could redefine the same storage; making the assignment unnecessary.
DEFINE WS-SERIAL W 5 N VALUE 234
DEFINE WS-SERIAL-NO WS-SERIAL 5 A
DISPLAY 'WS-SERIAL = ' WS-SERIAL
DISPLAY 'WS-SERIAL-NO = ' WS-SERIAL-NO
Results in the following:
WS-SERIAL = 234
WS-SERIAL-NO = 00234
The difference is that the default edit mask is applied to the numeric definition. (WS-SERIAL)
DEFINE WS-SERIAL W 5 N VALUE 234 MASK('99999')
DISPLAY 'WS-SERIAL = ' WS-SERIAL
Results in the following:
WS-SERIAL = 00234

Some useful Easytrieve notes:
Numeric data (N or P) defined without a number of decimal places coded is always stored as a positive integer using a sign nibble of x'F'
DEFINE WS-NUM W 6 N VALUE 100 contains hex 'F0F0F0F1F0F0'
DEFINE WS-NUM W 4 P VALUE 100 contains hex '0000100F'

Numeric data (N or P) defined with a number of decimal places coded is stored as above, but the field is signed. Positive numbers are stored as above, and negative numbers are stored with a sign nibble of x'D'.
DEFINE WS-NUM W 6 N 0 VALUE -100 contains hex 'F0F0F0F1F0D0'
DEFINE WS-NUM W 4 P 0 VALUE -100 contains hex '0000100D'

Just one thing that the manual doesn't tell you. Numeric data specified without indicating the number of decimal places is treated as character data in reports. (it does not get accumulated into control total lines) If you want integer totals in report control totals, you must code the number of decimal positions as 0 (zero)