85329c3 Aug 17, 2018
1 contributor

### Users who have contributed to this file

103 lines (87 sloc) 3.95 KB

## decimal4j

Java library for fast fixed-point arithmetic based on longs with support for up to 18 decimal places.

#### Features

• 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

#### Quick Start

###### Example 1: Interest Calculation
```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
``````
###### Example 2: Circumference of a Circle
```public class Circle {
public static void main(String[] args) {
Decimal18f PI = Decimal18f.valueOf(Math.PI);
System.out.println("Circumference with 5m radius is ~" + circ + "m");
System.out.println("Circumference with 5m radius is ~" + (2*Math.PI * 5) + "m");

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
``````
###### More examples

Can be found in the wiki.

###### Maven
```<dependency>
<groupId>org.decimal4j</groupId>
<artifactId>decimal4j</artifactId>
<version>1.0.3</version>
<scope>compile</scope>
</dependency>```
``````compile 'org.decimal4j:decimal4j:1.0.3'