ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - eriksencosta/math-common: Math conventions to reduce boilerplate code
Math conventions to reduce boilerplate code. Contribute to eriksencosta/math-common development by creating an account on GitHub.
Visit Site

GitHub - eriksencosta/math-common: Math conventions to reduce boilerplate code

GitHub - eriksencosta/math-common: Math conventions to reduce boilerplate code

Math Common

Codacy grade Codacy coverage

Math conventions to reduce boilerplate code.

Installation

Add Math Common to your Gradle build script:

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.eriksencosta.math:common:0.2.0")
}

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

<dependency>
    <groupId>com.eriksencosta.math</groupId>
    <artifactId>common</artifactId>
    <version>0.2.0</version>
</dependency>

Math Common is not compatible with the Android SDK at the moment.

Usage

Rounding

The library provides the Rounding strategy to make rounding easier:

1.25.round(1) // 1.3

By default, the RoundingMode.HALF_EVEN is used for rounding (it is the rounding logic commonly taught at school). If you want to use a different mode, create a Rounding and use it:

import java.math.RoundingMode

val rounding = Rounding.to(2, RoundingMode.FLOOR)

rounding.round(1.257) // 1.25
rounding.round(1.253) // 1.25

// Alternatively:

1.257.round(rounding) // 1.25
1.253.round(rounding) // 1.25

If you want to round the result of a calculation, use the overloaded round() method which accepts a function as parameter:

Rounding.to(2).round { 3.14159 * 10.0.squared() } // 314.16

Alternatively:

{ 3.14159 * 10.0.squared() }.round(2) // 314.16

Exponentiation

The extension functions squared and cubed are available for BigDecimal, Double, Float, Long, and Int:

2.squared() // 4
2.0.cubed() // 8.0

Performance

The library caches the Rounding objects by default. You can configure the cache by calling configureCache():

configureCache {
    maximumItems = 100
    expirationTime = 2
    expirationTimeUnit = TimeUnit.HOURS
}

Rounding.to(2) === Rounding.to(2) // true

The previous example will configure the cache to store up to 100 Rounding objects, and a cached object will live for up to two hours. If you want to disable the cache, call disableCache():

disableCache()

Rounding.to(2) === Rounding.to(2) // false

API documentation

Read the API documentation for further details.

License

The Apache Software License, Version 2.0

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory