Coder Social home page Coder Social logo

aaron-ds / decimal4j Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tools4j/decimal4j

0.0 0.0 0.0 2.26 MB

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

Home Page: decimal4j.org

License: MIT License

Java 95.72% HTML 0.01% FreeMarker 4.27%

decimal4j's Introduction

Build Status Maven Central Javadocs

decimal4j

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

Javadoc API

http://decimal4j.org/javadoc

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);
		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
More examples

Can be found in the wiki.

Maven/Gradle

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'

Download

You can download binaries, sources and javadoc from maven central:

FAQ

More Information

decimal4j's People

Contributors

terzerm avatar majerv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.