BikDecimal
A Kotlin Multiplatform library providing arbitrary-precision decimal arithmetic across Android, iOS, and JVM platforms.
Features
- Multiplatform Support: Works seamlessly on Android, iOS, and JVM
- Arbitrary Precision: Handle decimal numbers with high precision without floating-point errors
- Easy to Use: Kotlin operator overloading for natural arithmetic operations
- Type Safe: Strongly typed decimal numbers with platform-native implementations
Supported Platforms
- Android (using
java.math.BigDecimal)
- JVM (using
java.math.BigDecimal)
- iOS (using
NSDecimalNumber)
Installation
Add the dependency to your commonMain source set:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("jp.co.tanocee:bikdecimal:1.0.0")
}
}
}
Usage
Basic Arithmetic Operations
import jp.co.tanocee.bikdecimal.BikDecimal
val a = BikDecimal("123.45")
val b = BikDecimal("67.89")
val sum = a + b
val difference = a - b
val product = a * b
val quotient = a / b
Constructors
val fromString = BikDecimal("999.99")
val fromDouble = BikDecimal(3.14159)
val fromLong = BikDecimal(42L)
Constants
val zero = BikDecimal.ZERO
val one = BikDecimal.ONE
Comparison
val x = BikDecimal("100")
val y = BikDecimal("200")
when {
x < y -> println("x is less than y")
x > y -> println("x is greater than y")
x == y -> println("x equals y")
}
x.compareTo(y)
Conversions
val value = BikDecimal("123.456")
val asString = value.toPlainString()
val asDouble = value.toDouble()
val asLong = value.toLong()
Extension Functions
valid = .toBikDecimal()
invalid = .toBikDecimal()
custom = .toBikDecimal(BikDecimal.ONE)
( name: String, price: String)
products = listOf(
Product(, ),
Product(, ),
Product(, )
)
total = products.sumOf { it.price.toBikDecimal() }
Negative Values
val value = BikDecimal("42.5")
val negated = value.negative()
Sample Application
The sample module contains a Compose Multiplatform application demonstrating all features of BikDecimal. You can run it to see interactive examples of:
- Basic arithmetic operations
- Different constructor types
- Comparison operations
- Conversion methods
- Collection operations with
sumOf
Running the Sample App
Android:
./gradlew :sample:assembleDebug
iOS:
Open the project in Xcode and run the sample target.
Project Structure
- bikdecimal-core - The core library with multiplatform implementation
Building
To build the library:
./gradlew :bikdecimal-core:build
To build the entire project including the sample app:
./gradlew build
License
LICENSE