This blog was co-authored by:
Version 2.0 Released!
We are pleased to announce the release of Version 2.0 for the IBM Decimal Arithmetic Library for Java.
You can download it from Maven at Maven Central: com.ibm.jzos:decimal-arithmetic
See the License & Terms
1. What is the IBM Decimal Arithmetic Library?
The IBM Decimal Arithmetic Library for Java enables programmers to use decimal values and variables with extreme precision, as in languages such as COBOL and PL/I, which are known for their robust decimal arithmetic capabilities.
By utilizing this library, developers can ensure that their financial, monetary, or other precision-critical calculations are performed accurately and reliably, avoiding potential rounding errors that can occur with traditional binary floating-point representations.
As a result, Java applications can now handle complex financial transactions, statistical analysis, or other precision-sensitive tasks with the same level of precision as legacy systems written in COBOL or PL/I.
2. Product Features
2.1 Accurate Decimal Calculations
The IBM Decimal Arithmetic Library enables developers to accurately program applications that deal with decimal data, ensuring full compatibility with IBM COBOL on Z. The library helps avoid potential rounding errors that can occur with traditional binary floating-point representations.
2.2 Data Access Accelerator for High-Speed Z Performance
The IBM Decimal Arithmetic Library leverages the Data Access Accelerator (DAA), which uses the latest hardware decimal and vector-packed decimal instructions, to deliver accelerated performance on Z systems, ensuring high-performance decimal arithmetic operations.
2.3 Java-First Design
The library provides an idiomatic Java API, allowing developers to work with decimal arithmetic in a natural and familiar way, without a COBOL-like coding style.
3. How do I import it?
The recommended approach is to import the dependency via the Maven pom.xml file. You can do this by adding the following code block to your pom.xml:
<!-- Source: https://mvnrepository.com/artifact/com.ibm.jzos/decimal-arithmetic -->
<dependency>
<groupId>com.ibm.jzos</groupId>
<artifactId>decimal-arithmetic</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
Alternatively, there are several other means of downloading the dependency, including downloading the JAR file directly from Maven Central.
4. Why use the Arithmetic Library? (Interest Computation Example)
To demonstrate the Arithmetic Library's effectiveness, let's consider an interest computation example. We'll run the same calculation in COBOL, Java BigDecimal, and the Java Arithmetic Library. All three can produce COBOL's result. The difference is how much COBOL-specific behavior each one makes you reproduce by hand.
4.1 With z/OS Enterprise COBOL
The original COBOL implementation of the interest computation example is as follows:
IDENTIFICATION DIVISION.
PROGRAM-ID. INTEREST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 B PIC 9(15)V99 VALUE 9638071955.02.
01 C PIC 9(12)V99 VALUE 1809459311.54.
01 P PIC V9(6) VALUE .081090.
01 N PIC 999 VALUE 30.
01 MN PIC 99V9(6).
01 BP PIC $*(14)9.99.
01 BPC PIC $$$,$$$,$$$,$$$,$$9.99.
PROCEDURE DIVISION.
COMPUTE MN = (1 + P) ** N.
COMPUTE B = (B + C) * MN + C * ((MN - 1) / P - 1).
MOVE B TO BP.
DISPLAY BP.
MOVE B TO BPC.
DISPLAY BPC.
GOBACK.
END PROGRAM INTEREST.
The output of this COBOL program is shown below, demonstrating the accurate result obtained using this legacy language.
COBOL output:
$***326047440036.87
⎵⎵⎵$326,047,440,036.87
4.2 With Java BigDecimal (Default Settings)
A direct translation of the COBOL program to Java BigDecimal is shown below. It keeps BigDecimal's default behavior everywhere except the division, which needs an explicit scale.
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class InterestNoRoundingConst {
public static void main(String[] args) {
BigDecimal b = new BigDecimal(BigInteger.valueOf(963807195502L), 2);
BigDecimal c = new BigDecimal(BigInteger.valueOf(180945931154L), 2);
BigDecimal p = new BigDecimal(BigInteger.valueOf(81090), 6);
short n = 30;
// COMPUTE MN = (1 + P) ** N
BigDecimal mn = BigDecimal.ONE.add(p).pow(n);
// COMPUTE B = (B + C) * MN + C * ( (MN - 1) / P - 1) .
b = b.add(c).multiply(mn).add(
c.multiply(mn.subtract(BigDecimal.ONE).divide(p, 20, RoundingMode.DOWN).subtract(BigDecimal.ONE)));
System.out.printf("$%17.2f\n", b);
System.out.println(new DecimalFormat("$#,##0.##").format(b));
}
}
The output of this program differs from the COBOL result, yielding $326,047,467,304.71 instead of $326,047,440,036.87.
BigDecimal output:
$⎵⎵326047467304.71
$326,047,467,304.71
This gap comes from settings, not from a limit of BigDecimal. COBOL truncates values as it stores them into fields declared with a PICTURE clause, and BigDecimal keeps full precision unless you tell it otherwise. Section 4.3 shows the settings that close the gap.
4.3 With Java BigDecimal (COBOL-Matching Settings)
Three COBOL behaviors explain the difference above:
MN is declared PIC 99V9(6), so COBOL truncates it to six decimal places when it is stored.
- IBM Enterprise COBOL caps the intermediate division result at six decimal places.
B is declared PIC 9(15)V99, so COBOL truncates the final result to two decimal places when it is stored.
BigDecimal reproduces all three when you set the scale and rounding mode explicitly:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class InterestMatchesCobol {
public static void main(String[] args) {
BigDecimal b = new BigDecimal(BigInteger.valueOf(963807195502L), 2);
BigDecimal c = new BigDecimal(BigInteger.valueOf(180945931154L), 2);
BigDecimal p = new BigDecimal(BigInteger.valueOf(81090), 6);
short n = 30;
// COMPUTE MN = (1 + P) ** N, stored in PIC 99V9(6)
BigDecimal mn = BigDecimal.ONE.add(p).pow(n).setScale(6, RoundingMode.DOWN);
// Intermediate division result capped at 6 decimal places
BigDecimal ratio = mn.subtract(BigDecimal.ONE).divide(p, 6, RoundingMode.DOWN);
// COMPUTE B = (B + C) * MN + C * ( (MN - 1) / P - 1), stored in PIC 9(15)V99
b = b.add(c).multiply(mn).add(c.multiply(ratio.subtract(BigDecimal.ONE)))
.setScale(2, RoundingMode.DOWN);
System.out.printf("$%17.2f\n", b);
System.out.println(new DecimalFormat("$#,##0.00").format(b));
}
}
With these settings, BigDecimal matches the COBOL output:
BigDecimal output (COBOL-matching settings):
$⎵⎵326047440036.87
$326,047,440,036.87
The catch is that you need to know these COBOL rules and apply each one in the right place. Leaving out the MN truncation gives $326,047,466,796.50, and leaving out the division cap gives $326,047,441,749.26. Both versions compile and run without any error. Dropping the final setScale(2, RoundingMode.DOWN) happens to give the same output for this input, but it would diverge from COBOL whenever the discarded digits round up.
4.4 With Java Arithmetic Library
The IBM Decimal Arithmetic Library handles the storage rules through variable declarations, as shown below. A DAAVariable declared with a DAAPrecision truncates to that precision every time it is assigned, the same way a PICTURE clause does. The intermediate division cap is still explicit, through .truncate(6).
import com.ibm.arithmetic.decimal.daa.DAAVariable;
import com.ibm.arithmetic.decimal.daa.DAAPrecision;
import com.ibm.arithmetic.decimal.daa.DAAFactory;
import com.ibm.arithmetic.decimal.daa.DAADecimal;
import com.ibm.arithmetic.decimal.edit.Formatter;
public class TestInterestExample {
private static final DAAFactory df = DAAFactory.INSTANCE;
public static void main(String[] args) throws Exception {
DAAPrecision prec17s2 = new DAAPrecision(17, 2, true);
DAAPrecision prec8s6 = new DAAPrecision(8, 6, true);
DAAVariable b = df.createDecimalVariable(prec17s2, df.createDecimal(963807195502L, 2));
DAADecimal c = df.createDecimal(180945931154L, 2);
DAADecimal p = df.createDecimal(81090, 6);
DAADecimal n = df.createDecimal(30);
// COMPUTE MN = (1 + P) ** N
DAAVariable mn = df.createDecimalVariable(prec8s6, p.plus(1).toThePowerOf(n));
// COMPUTE B = (B + C) * MN + C * ( (MN - 1) / P - 1) .
b.set(b.plus(c).times(mn).plus(c.times(mn.minus(1).dividedBy(p).truncate(6).minus(1))));
System.out.println(b.format(Formatter.fromString("$*(14)9.99")));
System.out.println(b.format(Formatter.fromString("$$$,$$$,$$$,$$$,$$9.99")));
}
}
As demonstrated below, the Java Arithmetic Library produces the same accurate result as the original COBOL implementation, validating its effectiveness in handling precision-critical calculations.
Arithmetic library output:
$***326047440036.87
⎵⎵⎵$326,047,440,036.87
Why .truncate(6)? IBM Enterprise COBOL on z/OS silently caps intermediate division results to 6 decimal places. So without it, Java's default arbitrary precision quietly inflates the final figure by over $1,700 on a billion-dollar principal.
4.5 Comparing the Approaches
All three programs print $326,047,440,036.87. The table shows where each one handles the COBOL behaviors described in section 4.3.
| COBOL behavior |
Java BigDecimal |
Arithmetic Library |
Store MN in PIC 99V9(6) |
.setScale(6, RoundingMode.DOWN) |
DAAVariable declared with DAAPrecision(8, 6, true) |
| Cap intermediate division at 6 decimal places |
.divide(p, 6, RoundingMode.DOWN) |
.dividedBy(p).truncate(6) |
Store B in PIC 9(15)V99 |
.setScale(2, RoundingMode.DOWN) |
DAAVariable declared with DAAPrecision(17, 2, true) |
Edited output such as $$$,$$$,$$$,$$$,$$9.99 |
A hand-built DecimalFormat pattern |
Formatter.fromString with the same PICTURE string |
The Arithmetic Library declares precision once per variable, the way a PICTURE clause does, and it reuses COBOL PICTURE strings for output. That makes it easier to port COBOL logic to Java and to check the result against the original program.
On IBM Z, the library also runs decimal operations through the Data Access Accelerator described in section 2.2, which uses hardware decimal instructions. BigDecimal has no equivalent, so decimal-heavy workloads on Z should run faster with the library. Measure with your own workload to see the size of the gain.
Let's get started!
Download the IBM Decimal Arithmetic Library now and start building more accurate and reliable solutions.
Visit the Maven repository: Maven Central: com.ibm.jzos:decimal-arithmetic