decimal4j

repository·master·Indexed 41 days ago

https://github.com/tools4j/decimal4j

A high-performance Java library for fast fixed-point arithmetic based on unscaled longs, supporting up to 18 decimal places. It features zero-garbage computation APIs, MutableDecimal for chained operations, and support for all java.math.RoundingMode options.

Tokens
559
Snippets
2
Records
2
Agent score
2%

What's inside decimal4j

  1. Perform fixed-point arithmetic with decimal4j

    master

    decimal4j provides fixed-point arithmetic using unscaled long values, supporting between 0 and 18 decimal places. The scale is determined by the specific type used (e.g., Decimal2f for 2 decimal places, Decimal18f for 18).

    Key Features

    • Scale Management: The result of an operation usually inherits the scale of the primary operand.
    • Rounding: Supports all java.math.RoundingMode options. The default rounding mode is HALF_UP.
    • Overflow: Provides options to throw an exception when arithmetic overflow occurs.
    • Efficiency: Offers MutableDecimal for chained operations and a DecimalArithmetic API for zero-garbage computations using unscaled long values.
    // Example: Interest Calculation
    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);
    
    // Example: Using specific RoundingMode
    Decimal2f down = radius.multiplyBy(PI.multiply(2), RoundingMode.DOWN);
  2. Install decimal4j via Maven or Gradle

    master

    To use decimal4j in your Java project, add the following dependency to your build configuration file.

    Maven

    Add the dependency to your pom.xml:

    Gradle

    Add the dependency to your build.gradle using the compile configuration.

    <!-- Maven -->
    <dependency>
    	groupId>org.decimal4j</groupId>
    	<artifactId>decimal4j</artifactId>
    	<version>1.0.3</version>
    	<scope>compile</scope>
    </dependency>
    
    <!-- Gradle -->
    compile 'org.decimal4j:decimal4j:1.0.3'