Skip to main content

I have trouble computing two numbers where one can be a negative number. All numbers are kept as text.

What is happening...

All the values are kept as text in the field of type PIC X(...) VALUES SPACES.

I have a procedure that will find the start of the value. For example:

_ _ _ _ _ 2 3 4 . 32
_ _ _ _ _  19. 32  (can be also negative number)

 ( underscores are representing spaces)

Adding, subtracting, multiplying, dividing everything works fine with a positive number. IF I got a negative number the program will break.

MOVE "_ _ _ _ 2 3 4 . 3 2" TO NAZIV
PERFORM SEC-LEN
SEC-LEN will give me two informations 1) whare are the first number position from left - PCP
                                                                2) what is the length of the number - DUZ

For the above example, PCP is 5 and DUZ is 6 (five numbers + dot)

MOVE SPACE TO SLXMLDBF
STRING " <AMOUNT>" DELIMITED SIZE
NAZIV(PCP:DUZ) DELIMITED SIZE
"</AMOUNT>" DELIMITED SIZE INTO SLXMLDBF
WRITE SLXMLDBF

The above code will write in the file <AMOUNT>234.32</AMOUNT>

01 NUM-1 PIC 9(13) VALUE 0.
MOVE NAZIV(PCP:DUZ) TO NUM-1.
COMPUTE NUM-1  = NUM-1 + 10.

I tried to work with S9(...) type of fields for the calculation of positive and negative numbers but without results. All operation on two positive numbers works fine. The problem comming when I get a negative number (tried with S9 type of filed

Redefining text field also didn't work for me.

My question is ... If there is a negative number written as text, how can I put it in the S9 variable and then use it for additions with another positive/negative number converted also from text? 

I really hope that somebody can help me to overcome this problem!

Regards,
Ivan


#negativenumbers
#Text

I have trouble computing two numbers where one can be a negative number. All numbers are kept as text.

What is happening...

All the values are kept as text in the field of type PIC X(...) VALUES SPACES.

I have a procedure that will find the start of the value. For example:

_ _ _ _ _ 2 3 4 . 32
_ _ _ _ _  19. 32  (can be also negative number)

 ( underscores are representing spaces)

Adding, subtracting, multiplying, dividing everything works fine with a positive number. IF I got a negative number the program will break.

MOVE "_ _ _ _ 2 3 4 . 3 2" TO NAZIV
PERFORM SEC-LEN
SEC-LEN will give me two informations 1) whare are the first number position from left - PCP
                                                                2) what is the length of the number - DUZ

For the above example, PCP is 5 and DUZ is 6 (five numbers + dot)

MOVE SPACE TO SLXMLDBF
STRING " <AMOUNT>" DELIMITED SIZE
NAZIV(PCP:DUZ) DELIMITED SIZE
"</AMOUNT>" DELIMITED SIZE INTO SLXMLDBF
WRITE SLXMLDBF

The above code will write in the file <AMOUNT>234.32</AMOUNT>

01 NUM-1 PIC 9(13) VALUE 0.
MOVE NAZIV(PCP:DUZ) TO NUM-1.
COMPUTE NUM-1  = NUM-1 + 10.

I tried to work with S9(...) type of fields for the calculation of positive and negative numbers but without results. All operation on two positive numbers works fine. The problem comming when I get a negative number (tried with S9 type of filed

Redefining text field also didn't work for me.

My question is ... If there is a negative number written as text, how can I put it in the S9 variable and then use it for additions with another positive/negative number converted also from text? 

I really hope that somebody can help me to overcome this problem!

Regards,
Ivan


#negativenumbers
#Text

Here is the full example:

01 br1 pic s9(5)v99.
01 br11 pic -9(5).99.

01 txtbr1 REDEFINES br11 PIC X(8).

01 br2 pic s9(5)v99.
01 br22 pic -9(5).99.

01 txtbr2 REDEFINES br22 PIC X(8).

move "-123.32" to txtbr1.

MOVE SPACE TO SLXMLDBF.
STRING " <AMOUNT_1>" DELIMITED SIZE
br11 DELIMITED SIZE
"</AMOUNT_1>" DELIMITED SIZE INTO SLXMLDBF.
WRITE SLXMLDBF.

MOVE "23.43" TO txtbr2.

MOVE SPACE TO SLXMLDBF.
STRING " <AMOUNT_2>" DELIMITED SIZE
br22 DELIMITED SIZE
"</AMOUNT_2>" DELIMITED SIZE INTO SLXMLDBF.
WRITE SLXMLDBF.

COMPUTE br2 = br2 + br1.

MOVE SPACE TO SLXMLDBF.
STRING " <AMOUNT_3>" DELIMITED SIZE
txtbr2 DELIMITED SIZE
"</AMOUNT_3>" DELIMITED SIZE INTO SLXMLDBF.
WRITE SLXMLDBF.

The above code will give the output
<AMOUNT_1>-123.32 </AMOUNT_1>
<AMOUNT_2>23.43 </AMOUNT_2>

The program is crashing because of this line
COMPUTE br2 = br2 + br1.


Here is the full example:

01 br1 pic s9(5)v99.
01 br11 pic -9(5).99.

01 txtbr1 REDEFINES br11 PIC X(8).

01 br2 pic s9(5)v99.
01 br22 pic -9(5).99.

01 txtbr2 REDEFINES br22 PIC X(8).

move "-123.32" to txtbr1.

MOVE SPACE TO SLXMLDBF.
STRING " <AMOUNT_1>" DELIMITED SIZE
br11 DELIMITED SIZE
"</AMOUNT_1>" DELIMITED SIZE INTO SLXMLDBF.
WRITE SLXMLDBF.

MOVE "23.43" TO txtbr2.

MOVE SPACE TO SLXMLDBF.
STRING " <AMOUNT_2>" DELIMITED SIZE
br22 DELIMITED SIZE
"</AMOUNT_2>" DELIMITED SIZE INTO SLXMLDBF.
WRITE SLXMLDBF.

COMPUTE br2 = br2 + br1.

MOVE SPACE TO SLXMLDBF.
STRING " <AMOUNT_3>" DELIMITED SIZE
txtbr2 DELIMITED SIZE
"</AMOUNT_3>" DELIMITED SIZE INTO SLXMLDBF.
WRITE SLXMLDBF.

The above code will give the output
<AMOUNT_1>-123.32 </AMOUNT_1>
<AMOUNT_2>23.43 </AMOUNT_2>

The program is crashing because of this line
COMPUTE br2 = br2 + br1.

You could use the intrinsic function NUMVAL and you can avoid all of this code,

It will return the numeric value of an alphanumeric field containing a numeric value.

Example:

compute br2 = function numval(txtbr2)  + function numval(txtbr1)


You could use the intrinsic function NUMVAL and you can avoid all of this code,

It will return the numeric value of an alphanumeric field containing a numeric value.

Example:

compute br2 = function numval(txtbr2)  + function numval(txtbr1)

Chris, as earlier your information was very helpful. Thanks a lot! It works!

I have one additional question related to this.

The format of the number was changed... There is not DOT but the type of the field br1/br2 is S9(11)V99 and br11/br22 -9(11)V99

<AMOUNT_1>25742.90 </AMOUNT_1>
<AMOUNT_2>69770.70 </AMOUNT_2>

COMPUTE br22 = function numval(txtbr1) + function numval(txtbr2)

STRING " <AMOUNT_3>" DELIMITED SIZE
txtbr2 DELIMITED SIZE
"</AMOUNT_3>" DELIMITED SIZE INTO SLXMLDBF
WRITE SLXMLDBF


<AMOUNT_3> 9551360 </AMOUNT_3>

Why is this happening? How to overcome this problem?

Regards,

Ivan


Chris, as earlier your information was very helpful. Thanks a lot! It works!

I have one additional question related to this.

The format of the number was changed... There is not DOT but the type of the field br1/br2 is S9(11)V99 and br11/br22 -9(11)V99

<AMOUNT_1>25742.90 </AMOUNT_1>
<AMOUNT_2>69770.70 </AMOUNT_2>

COMPUTE br22 = function numval(txtbr1) + function numval(txtbr2)

STRING " <AMOUNT_3>" DELIMITED SIZE
txtbr2 DELIMITED SIZE
"</AMOUNT_3>" DELIMITED SIZE INTO SLXMLDBF
WRITE SLXMLDBF


<AMOUNT_3> 9551360 </AMOUNT_3>

Why is this happening? How to overcome this problem?

Regards,

Ivan

The symbols "S" and "V" are placeholders for an implied sign and decimal point in a number. Fields using these symbols do not actually contain a "+" or "-" sign or the decimal point ".". These type of fields are used for calculations. 

If you want the actual decimalo point and sign chacters to be displayed then you would use the numeric editing characters either "+" "-" or "."

01 num-field  pic s9(4)v99  value -123.45.
01 edit-field   pic +9(4).99.

move num-field to edit-field
display edit-field
-123.45


The symbols "S" and "V" are placeholders for an implied sign and decimal point in a number. Fields using these symbols do not actually contain a "+" or "-" sign or the decimal point ".". These type of fields are used for calculations. 

If you want the actual decimalo point and sign chacters to be displayed then you would use the numeric editing characters either "+" "-" or "."

01 num-field  pic s9(4)v99  value -123.45.
01 edit-field   pic +9(4).99.

move num-field to edit-field
display edit-field
-123.45

Chris,

as I said earlier I am learning Cobol in the movement. I didn't know that fact! I thought that if we want to represent and work with decimal numbers we must use S9(...)V9(...) type of field.


Again, I must say thanks a lot for sharing this information with me! It was very useful - I solved all the problems which I had. 🙂