Validating numeric fields in cobol
In my coding style, I only indent two spaces under EVALUATE to put in my WHENs. Such as if the IF statement is structured so the most likely true statement is first would the evaluate still be faster?I then indent two more spaces for the statement(s) to be performed for each WHEN. For example, if a sales app calculates shipping cost depending on the customer's total purchase and shipping method, but gives a 1-grade discount to their prime customers, it could look something like this: EVALUATE TOTAL-PURCHASE ALSO MAIL-RATE ALSO PRIME-CUSTOMER WHEN 0.01 THRU 24.99 ALSO ' Ground' ALSO FALSE WHEN 0.01 THRU 24.99 ALSO '2nd day Air' ALSO TRUE MOVE 2.75 TO SHIPPING-COST WHEN 0.01 THRU 24.99 ALSO '2nd day Air' ALSO FALSE WHEN 0.01 THRU 24.99 ALSO ' Next day Air' ALSO TRUE MOVE 5.95 TO SHIPPING-COST WHEN 25.00 THRU 49.99 ALSO ' Ground' ALSO FALSE WHEN 25.00 THRU 44.99 ALSO '2nd day Air' ALSO TRUE ...Of course, this can also be done with IFs, but there are 2 advantages to using the EVALUATE: repeated indenting isn't necessary and the structure becomes very similar to that of a truth-table which (IMO) is far easier to comprehend and maintain.
The first four bits of each byte are called the “zone” and the last four bits the “digit”.
Note: The code accompanying this article is available for download here, and it has been updated since it originally ran [Updated 06/13/07] I have seen requests many times in the forums from programmers asking how best to handle data with invalid decimal data.
Typically this type of error is first discovered when a program ends abnormally.
I’ve developed a command to identify and (optionally) “fix” errant data. Of course in the case of a zoned-decimal field such as this one, it is acceptable for the last (and only the last) position to contain a value that appears to be alphabetic.
This is because the last position of the field identifies the sign.
This puts the statement(s) to be performed in the same column in which they would have been if the old IF-construct had been used.