Strings and edited output
A report is read by a person, and the part of a language that decides how a number looks is the part a report programmer spends the most time with. COBOL has it in the data division: a field with an edited picture is a number plus a shape, and a MOVE into it does the formatting. C++ has std::format, which is good at widths, precision and padding and has no idea what a debit is. Both programs below print the same eight lines.
The report line in COBOL
Five edited pictures, each one a different piece of a report: a currency amount with a floating dollar sign and commas, a debit with DB where a credit would print nothing, a percentage, a count with zero suppression and one with leading zeros, and a name that is a fixed twenty characters.
IDENTIFICATION DIVISION.
PROGRAM-ID. FORMAT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-AMOUNT PIC 9(9)V99 VALUE 1234567.89.
01 WS-DEBIT PIC S9(7)V99 VALUE -1234.56.
01 WS-RATE PIC 99V9999 VALUE 13.5000.
01 WS-NAME PIC X(20) VALUE "Ada Lovelace".
01 WS-COUNT PIC 9(5) VALUE 42.
01 WS-MONEY PIC $ZZZ,ZZZ,ZZ9.99.
01 WS-DB PIC Z(3),ZZ9.99DB.
01 WS-PCT PIC ZZ9.99.
01 WS-CNT PIC ZZZZ9.
PROCEDURE DIVISION.
MOVE WS-AMOUNT TO WS-MONEY
DISPLAY "money [" WS-MONEY "]"
MOVE WS-DEBIT TO WS-DB
DISPLAY "debit [" WS-DB "]"
MOVE WS-RATE TO WS-PCT
DISPLAY "rate [" WS-PCT "%]"
MOVE WS-COUNT TO WS-CNT
DISPLAY "count [" WS-CNT "]"
DISPLAY "count0 [" WS-COUNT "]"
DISPLAY "name [" WS-NAME "]"
MOVE "Katherine Johnson-Longname" TO WS-NAME
DISPLAY "long [" WS-NAME "]"
DISPLAY "len [" FUNCTION LENGTH(WS-NAME) "]"
STOP RUN.
cobc -x -free -Wall -O2 -o format format.cob
./format
echo $?
money [$ 1,234,567.89]
debit [ 1,234.56DB]
rate [ 13.50%]
count [ 42]
count0 [00042]
name [Ada Lovelace ]
long [Katherine Johnson-Lo]
len [20]
0
The build prints one warning, and it is worth keeping in the page because it is the compiler doing the job a C++ compiler also does: value size exceeds data size at the long name, because the literal is twenty-six characters and the field twenty. The program still runs, the name is cut, and the warning is the only thing that says so before a reader notices a truncated surname in a report.
The report line in C++
The C++ program uses std::format for what it does well and writes the money and the debit by hand: the thousands separator for a floating-point value is not available in this standard library, and no format string turns a negative number into the financial parenthesis or the DB suffix. Both are a helper function of ten lines.
#include <cstdio>
#include <format>
#include <string>
std::string grouped(long long cents) {
std::string digits = std::format("{:d}", cents < 0 ? -cents : cents);
std::string out;
int since = 0;
for (auto it = digits.rbegin(); it != digits.rend(); ++it) {
if (since == 3) {
out.push_back(',');
since = 0;
}
out.push_back(*it);
++since;
}
std::string result(out.rbegin(), out.rend());
return (cents < 0 ? "-" : "") + result;
}
std::string money(long long cents) {
long long units = (cents < 0 ? -cents : cents) / 100;
long long part = (cents < 0 ? -cents : cents) % 100;
return (cents < 0 ? "-$" : "$") + grouped(units) + std::format(".{:02d}", part);
}
int main() {
long long amount = 123456789;
long long debit = -123456;
double rate = 13.5000;
int count = 42;
std::string name = "Ada Lovelace";
std::printf("money [%s]\n", std::format("{:>16}", money(amount)).c_str());
std::printf("debit [%s]\n",
std::format("{:>10}.{:02d}DB", grouped(debit / 100), -debit % 100).c_str());
std::printf("rate [%s%%]\n", std::format("{:>6.2f}", rate).c_str());
std::printf("count [%s]\n", std::format("{:>5}", count).c_str());
std::printf("count0 [%s]\n", std::format("{:05d}", count).c_str());
std::printf("name [%-20s]\n", name.c_str());
std::string long_name = "Katherine Johnson-Longname";
long_name.resize(20);
std::printf("long [%s]\n", long_name.c_str());
std::printf("len [%s]\n", std::format("{}", long_name.size()).c_str());
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o format_cpp format.cpp
./format_cpp
echo $?
money [ $1,234,567.89]
debit [ -1,234.56DB]
rate [ 13.50%]
count [ 42]
count0 [00042]
name [Ada Lovelace ]
long [Katherine Johnson-Lo]
len [20]
0
The format option that is not there
The standard has a grouping option for integers and, since C++23, for floating point: {:,.2f}. This standard library does not implement the second, and the compiler says so at compile time because the format string is checked:
#include <cstdio>
#include <format>
int main() {
std::printf("[%s]\n", std::format("{:,.2f}", 1234567.89).c_str());
return 0;
}
g++ -std=c++23 -Wall -Wextra -o /dev/null grouping.cpp
grouping.cpp: In function 'int main()':
grouping.cpp:5:39: error: call to consteval function 'std::basic_format_string<char, double>("{:,.2f}")' is not a constant expression
5 | std::printf("[%s]\n", std::format("{:,.2f}", 1234567.89).c_str());
| ^~~~~~~~~
This is the difference in kind between the two facilities. A COBOL edited picture is part of the language and every compiler that accepts the program formats it the same way; a C++ format string depends on the library, and a library that lags the standard is a build error today and a silent help text tomorrow. The C++ program above pays for it with a helper; a COBOL programmer pays for the same features with a picture of eight characters.
What each side had to write
| Line | COBOL | C++ |
|---|---|---|
| currency, commas | PIC $ZZZ,ZZZ,ZZ9.99 | a grouping helper and a format width |
| debit with DB | PIC Z(3),ZZ9.99DB | a sign test and a suffix by hand |
| percentage | PIC ZZ9.99 | {:>6.2f} and a literal % |
| zero-padded count | PIC 9(5) (all digits) | {:05d} |
| suppressed count | PIC ZZZZ9 | {:>5} |
| name cut at 20 | PIC X(20) and a warning | resize(20) and nothing |
The table is the honest summary. COBOL wins on the shapes that reports need: a picture says currency, debit, zero suppression and padding in one line, and every program that moves a number into it gets the same text. C++ wins on everything a report does not need and a program does: arbitrary widths, types, locales and a format language that composes, with the cost that the financial shapes are yours to write, and that a library gap is yours to find.
The COBOL lessons for this ground are edited numeric output and string intrinsic functions. The next page leaves the output and asks what happens to either program when a field changes after ten years.
The COBOL was built with cobc (GnuCOBOL) 3.2.0 and the C++ with g++ (Alpine 15.2.0) 15.2.0, both with -O2, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4.
Article author: Arthur Isaev