DFP is implementation of IEEE 754-2008 Decimal64 for Java/.NET/C/C++.
"...it is a bad idea to use floating point to try to represent exact quantities like monetary amounts. Using floating point for dollars-and-cents calculations is a recipe for disaster. Floating point numbers are best reserved for values such as measurements, whose values are fundamentally inexact to begin with." — Brian Goetz
Java lacks built-in type to represent Money or Quantity properties frequently used in financial domain.
Ideal data type for this purpose:
- Use base-10 (rather than base-2) to accurately represent monetary values
- Support wide range of values (ranging from hundred of billions of that represent portfolio values to fractions of 6 to 8 below decimal point to represent smallest tick sizes)
- Allow GC-free arithmetic (Garbage Collection pauses are evil in low-latency systems). This most likely implies using primitive data types.
- fast (as fast as non-builtin numeric data type could be)
- Support efficient conversion to String and double
DFP uses Java long primitive type to represent base-10 floating point numbers. DFP is based on IEEE 754-2008 standard and supports up to 16 significant decimal digits.
- Java - pure Java implementation (since version 0.12). Supported on all platforms where Java is supported.
- .NET - wrapper over C implementation with some functions re-written in C#. Supported platforms:
- x86-64 (Windows, Linux, Mac)
- x86 (Windows, Linux)
- arm64 (Linux, Mac)
- arm7 (Linux)
- C/C++ - based on Intel Decimal Floating Point Math Library with some additional APIs (like string parsing and specialized math functions).
- Please see DFP on Conan for supported platforms.
Add dependency (Gradle):
implementation 'com.epam.deltix:dfp:1.0.10'Use (allocation free):
import com.epam.deltix.dfp.Decimal64Utils;
@Decimal long price = Decimal64Utils.parse("123.45");
@Decimal long halfPrice = Decimal64Utils.divideByInteger(price, 2);
System.out.println(Decimal64Utils.toString(halfPrice));
System.out.println(Decimal64Utils.toScientificString(halfPrice));
System.out.println(Decimal64Utils.toFloatString(halfPrice));With value type wrapper (allocation on object creation):
import com.epam.deltix.dfp.Decimal64;
Decimal64 price = Decimal64.parse("123.45");
Decimal64 halfPrice = price.divide(Decimal64.fromLong (2));
System.out.println(halfPrice.toString());
System.out.println(halfPrice.toScientificString());
System.out.println(halfPrice.toFloatString());DFP was inspired on Intel Decimal Floating-Point Math Library that is written in C and provides implementation for IEEE 754-2008. Early DFP versions used JNI wrappers for this Intel library. Starting from the release 0.12 DFP for Java does not depend on native code.
This project was developed by Deltix developers Vitali Haravy, Boris Chuprin, Andrei Davydov.
This software uses Intel Decimal Floating Point Math Library.
This library is released under Apache 2.0 license. See (license)