Skip to content

eriksencosta/math-percentage

Repository files navigation

Percentage

Codacy grade Codacy coverage

Percentage calculations made easy.

Installation

Add Percentage to your Gradle build script:

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.eriksencosta.math:percentage:0.1.0")
}

If you're using Maven, add to your POM xml file:

<dependency>
    <groupId>com.eriksencosta.math</groupId>
    <artifactId>percentage</artifactId>
    <version>0.1.0</version>
</dependency>

Usage

The library provides the Percentage type: an immutable and thread-safe class that makes percentage calculations easy.

val percentage = 5.5.percent() // 5.5%

150 * percentage          // 8.25
150 decreaseBy percentage // 141.75
150 increaseBy percentage // 158.25

Other convenience functions are also available. To create a percentage based on a ratio, use the ratioOf function:

1 ratioOf 4 // 25%

To calculate the relative percentage change between two numbers, use the relativeChange function:

1 relativeChange 3 // 200%

And to discover the base value of a number when its represents a given percentage, use the valueWhen function:

5 valueWhen 20.percent() // 25.0 (i.e., 5 is 20% of 25)

API documentation

Read the API documentation for further details.

License

The Apache Software License, Version 2.0