A Kotlin Multiplatform library providing arbitrary-precision decimal arithmetic across Android, iOS, and JVM platforms.
- 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
- Android (using
java.math.BigDecimal) - JVM (using
java.math.BigDecimal) - iOS (using
NSDecimalNumber)
Add the dependency to your commonMain source set:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("jp.co.tanocee:bikdecimal:1.0.0")
}
}
}import jp.co.tanocee.bikdecimal.BikDecimal
val a = BikDecimal("123.45")
val b = BikDecimal("67.89")
val sum = a + b // 191.34
val difference = a - b // 55.56
val product = a * b // 8382.2205
val quotient = a / b // 1.818...// From String
val fromString = BikDecimal("999.99")
// From Double
val fromDouble = BikDecimal(3.14159)
// From Long
val fromLong = BikDecimal(42L)val zero = BikDecimal.ZERO // 0
val one = BikDecimal.ONE // 1val 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")
}
// Or use compareTo directly
x.compareTo(y) // Returns negative, zero, or positiveval value = BikDecimal("123.456")
val asString = value.toPlainString() // "123.456"
val asDouble = value.toDouble() // 123.456
val asLong = value.toLong() // 123// String to BikDecimal with default value
val valid = "100.5".toBikDecimal() // BikDecimal("100.5")
val invalid = "invalid".toBikDecimal() // BikDecimal.ZERO (default)
val custom = "invalid".toBikDecimal(BikDecimal.ONE) // BikDecimal.ONE
// Sum of collection
data class Product(val name: String, val price: String)
val products = listOf(
Product("Apple", "1.20"),
Product("Banana", "0.80"),
Product("Orange", "1.50")
)
val total = products.sumOf { it.price.toBikDecimal() } // 3.50val value = BikDecimal("42.5")
val negated = value.negative() // -42.5The 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
Android:
./gradlew :sample:assembleDebugiOS:
Open the project in Xcode and run the sample target.
- bikdecimal-core - The core library with multiplatform implementation
commonMain- Common API and extension functionsandroidMain- Android implementation usingjava.math.BigDecimaljvmMain- JVM implementation usingjava.math.BigDecimaliosMain- iOS implementation usingNSDecimalNumber
- sample - Sample application demonstrating library usage
To build the library:
./gradlew :bikdecimal-core:buildTo build the entire project including the sample app:
./gradlew build