COBOL to Java Migration: Rounding, Packed Decimal, and Sort Order Differences That Change Results

Translated code can be logically correct and still produce different payment amounts and differently ordered files. Where COBOL arithmetic and EBCDIC sorting differ from Java, C#, and Python defaults, and how a government program can test for it.

A COBOL program that calculates a payment and a Java program translated from it can follow the same steps and still disagree by one cent. The translation is faithful line by line. The difference comes from rules that COBOL applies without being asked: how many decimal places a field keeps, what happens to the digits that do not fit, and what order characters sort in.

These differences are small per record and large in aggregate, and they tend to surface late, during parallel testing or after go-live. This post lists the ones that matter most when a government system moves off a mainframe, shows an example, and describes how to test for them.

Why this matters for government systems

In GAO-25-107795, the Government Accountability Office (GAO) reviewed the 11 federal legacy systems most in need of modernization and reported that eight of them use outdated languages. The report names Common Business Oriented Language (COBOL) and assembly language, and notes that agencies have had difficulty finding staff who know them.

Systems of this kind calculate benefits, taxes, pay, and interest. Their outputs are amounts owed to or by a person, and many of those amounts are defined in statute or regulation. A one-cent difference in such a system is a changed payment, and someone outside the engineering team has to be able to explain it.

Where COBOL arithmetic differs from the target language

COBOL programs usually hold money in fixed-point decimal fields. A declaration such as PIC S9(7)V99 COMP-3 describes a signed number with seven integer digits and two decimal places, stored in packed decimal format. The field’s size is part of the calculation: every result is fitted to it.

The IBM Enterprise COBOL for z/OS 6.4 Language Reference states the rules. When a result has more decimal places than the receiving field, “truncation occurs unless ROUNDED is specified.” When ROUNDED is specified, the last kept digit is increased by 1 whenever the first discarded digit is 5 or greater. When a result is too large for the field and the statement has no ON SIZE ERROR phrase, “truncation rules apply” and the program continues.

Modern languages make different choices, and each one is documented.

BehaviorCOBOL (IBM Enterprise COBOL)Common target-language behavior
Extra decimal placesDropped, unless the statement says ROUNDEDJava BigDecimal throws an ArithmeticException if no rounding mode is given and the exact result cannot be represented (Oracle documentation)
A value exactly halfway, such as 10.525 to two placesWith ROUNDED, goes up in magnitude: 10.53C# Math.Round rounds midpoints to the nearest even digit by default: 10.52 (Microsoft documentation). Python’s decimal module has the same default (Python documentation)
Negative halfway values, such as -2.5 to a whole numberWith ROUNDED: -3Java Math.round rounds ties toward positive infinity: -2 (Oracle documentation)
Result too large for the fieldStored truncated when there is no ON SIZE ERROR phraseBigDecimal has no fixed size and keeps the whole value, so the difference appears later, in a fixed-width file or a database column
Decimal fractionsExact in packed and zoned decimal fieldsBinary floating point cannot hold 0.1 exactly. Oracle’s documentation notes that new BigDecimal(0.1) equals 0.1000000000000000055511151231257827021181583404541015625

Two further points follow from the table.

First, the order of rounding matters. A COBOL program that stores an intermediate value in a two-decimal field has truncated or rounded at that step. A translation that carries full precision through the calculation and rounds once at the end is doing different arithmetic.

Second, a translator that maps a money field to a double introduces errors that were never in the original. Decimal types (BigDecimal in Java, decimal in C#, Decimal in Python) avoid that, but only when the scale and rounding mode are set explicitly at each assignment, to match the receiving field in the source.

Sort order: EBCDIC and ASCII

Mainframe COBOL programs normally compare and sort text using the EBCDIC character encoding. Most target platforms use ASCII or Unicode order. The two sequences are different: EBCDIC places lowercase letters first, then uppercase letters, then digits. ASCII places digits first, then uppercase, then lowercase.

Oracle’s COBOL converter reference describes the consequence: for data that mixes letters and digits, behavior can differ “because letters sort before digits in EBCDIC and after digits in ASCII.” It adds that the converter cannot handle the issue, because it depends on the contents of the data and not on how fields are declared.

This affects more than the order of a printed report. Many batch programs read two sorted files and match records by key, assuming both files are in the same sequence. If one file was sorted on the mainframe and the other on the new platform, keys such as A1047 and 10472 are in opposite order, and the match step skips records without raising an error. The same applies to any comparison in the code that tests whether one key is less than another.

An illustrative example: a monthly payment calculation

The program and figures below are illustrative. A legacy program computes a monthly supplement as a base amount multiplied by a factor and stores it in a two-decimal field. The original statement has no ROUNDED phrase.

Base amountFactorExact productLegacy COBOL (truncates)Translation using round-half-evenDifference
1,250.500.062578.1562578.1578.16+0.01
842.000.012510.52510.5210.520.00
2,000.000.0625125.00125.00125.000.00

The translation is arguably more accurate, and the unit tests written against the new code pass. Suppose a parallel run covers 400,000 payments and the discarded digits are spread evenly. About half of the results then have a third decimal digit of 5 or more, so about 200,000 payments differ from the legacy output by one cent. That is about $2,000 a month in aggregate, with no line of code that looks wrong.

The team now has to decide whether the legacy truncation was the intended rule or an old defect. That decision changes what people are paid, so it belongs to the program owner, with the regulation in hand. The developer’s job is to surface the difference and reproduce the legacy result until told otherwise.

Reproduce by default, and record every exception

A workable rule is that the migrated system reproduces legacy results exactly, including results that look wrong, unless a named person has approved a change. Each approved change is recorded with the statement it affects, the old and new behavior, the number of records affected in test, and the approver.

This keeps two activities apart: moving the system, and changing what it computes. When they are mixed, a difference found in parallel testing could be a migration defect or an intended correction, and nobody can tell which without rereading the code.

How to test for it

General regression tests rarely catch these differences, because typical test values do not land on the boundaries. Targeted tests do.

  • Inventory the arithmetic. List every arithmetic statement with its receiving field, its decimal places, and whether it is ROUNDED or has an ON SIZE ERROR phrase. This can be extracted from the source.
  • Generate boundary inputs. For each statement, include inputs that produce an exact halfway value, a discarded digit of 4 and of 5, a negative result, a zero, and a value one digit too large for the field.
  • Compare at field level. Run the same inputs through both systems and compare every output field exactly. Tolerances hide the errors this test exists to find.
  • Test sorts and comparisons with mixed keys. Include keys with uppercase, lowercase, digits, spaces, and special characters, and compare record order as well as content.
  • Run on production-scale data. A full cycle of real inputs finds the combinations that nobody thought to write. Our post on data migration cutover and reconciliation covers record-level comparison in more detail.

The approach in our post on behavior preservation applies here: capture what the old system does before changing it, and treat any difference as a finding that needs a decision.

How QuantumWorks approaches this

PRISM analyzes a legacy codebase and maps its entry points, behavior, dependencies, and outputs, each anchored to file and line. That gives a team a starting list of where a program computes and compares values, and proposed changes pass through gates for compilation, tests, and behavioral equivalence, with a person approving or rejecting each one.

If your program is moving calculations off a mainframe and needs to show that the results did not change, we would be glad to talk. Contact QuantumWorks.

  • COBOL to Java Migration
  • Packed Decimal
  • Rounding
  • EBCDIC
  • Mainframe Modernization
  • Verification
  • Legacy Systems

Working on a hard technical problem?

We work with government organizations, research institutions, and technology teams across cybersecurity, software, and autonomous systems.