Here’s the translation of the number parsing example from Go to COBOL, formatted in Markdown suitable for Hugo:
Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in COBOL.
In COBOL, number parsing is handled differently from Go:
For floating-point numbers, we can use a simple MOVE statement to convert a string to a number.
For integer parsing, we use the COMPUTE verb along with the FUNCTION NUMVAL, which converts a string to its numeric value.
COBOL doesn’t have built-in support for hexadecimal parsing like Go’s ParseInt. We’d need to implement a custom routine for this (shown as CONVERT-HEX-TO-DEC in the example).
There’s no direct equivalent to ParseUint in COBOL. Unsigned integers are typically handled using regular numeric fields.
The FUNCTION NUMVAL serves a similar purpose to Go’s Atoi, converting a string to its numeric value.
Error handling in COBOL is typically done using condition names like ON SIZE ERROR, rather than returning error values.
To run this COBOL program:
Note that the exact output and behavior may vary depending on the COBOL compiler and runtime environment used.
Next, we’ll look at another common parsing task: URLs.