Java library for fast fixed-point arithmetic based on longs with support for up to 18 decimal places.
- Fixed-point arithmetic with 0 to 18 decimal places
- Implementation based on unscaled long value
- Option to throw an exception when an arithmetic overflow occurs
- Scale
- Type of a variable defines the scale (except for wildcard types)
- Result has usually the same scale as the primary operand (also for multiplication and division)
- Type Conversion
- Efficient conversion from and to various other number types
- Convenience methods to directly inter-operate with other data types (long, double, ...)
- All rounding modes supported (default: HALF_UP)
- Efficiency
- Fast and efficient implementation (see performance benchmarks)
MutableDecimalimplementation for chained operationsDecimalArithmeticAPI for zero-garbage computations (with unscaled long values)
public class Interest { public static void main(String[] args) { Decimal2f principal = Decimal2f.valueOf(9500); Decimal3f rate = Decimal3f.valueOf(0.067); Decimal2f time = Decimal2f.valueOf(1).divide(4); Decimal2f interest = principal.multiplyBy(rate.multiplyExact(time)); System.out.println("First quarter interest: $" + interest); } }will output
First quarter interest: $159.13 public class Circle { public static void main(String[] args) { Decimal18f PI = Decimal18f.valueOf(Math.PI); Decimal2f radius = Decimal2f.valueOf(5); Decimal2f circ = radius.multiplyBy(PI.multiply(2)); System.out.println("Circumference with 5m radius is ~" + circ + "m"); System.out.println("Circumference with 5m radius is ~" + (2*Math.PI * 5) + "m"); Decimal2f down = radius.multiplyBy(PI.multiply(2), RoundingMode.DOWN); System.out.println("Circumference with 5m radius is larger than " + down + "m"); } }will output
Circumference with 5m radius is ~31.42m Circumference with 5m radius is ~31.41592653589793m Circumference with 5m radius is larger than 31.41m Can be found in the wiki.
<dependency> <groupId>org.decimal4j</groupId> <artifactId>decimal4j</artifactId> <version>1.0.3</version> <scope>compile</scope> </dependency>compile 'org.decimal4j:decimal4j:1.0.3' You can download binaries, sources and javadoc from maven central:
- Javadoc API
- Wiki
- More Examples
- Zero-garbage Computations (with unscaled long values)
- Test Coverage
- Performance Benchmarks