Tiny library for handling money amounts predictably with decimal arithmetic, enforcing non-negative values, two decimal precision, rounding, arithmetic operations, and simple formatting.
A tiny Kotlin Multiplatform library for working with money amounts in a predictable, base‑10 way.
Currency provides a small, opinionated value type — CurrencyAmount — built on top of the Decimal library (au.lovecraft:decimal). It focuses on common needs for prices and totals: non‑negative amounts, cents precision (2 fractional digits), explicit rounding, arithmetic helpers, and simple platform‑aware formatting.
Note: This is an early, functionally incomplete draft. The API may change.
Configured in this module’s Gradle build:
Kotlin version: 2.2.20
This library is not yet published to a public repository. Recommended ways to consume it today:
Example using an included build:
settings.gradle.kts in your app project:
includeBuild("../currency") // path where this repo lives
Then in your module’s build.gradle.kts:
dependencies {
implementation(project(":currency"))
}
Notes:
// Construct
val a = CurrencyAmount.fromString("12.345")!! // -> 12.35 (HalfEven to 2 dp)
val b = CurrencyAmount.fromCents(1235u) // -> 12.35
val c = 99.dollars // from Int extension
val d = "1_234.00".replace("_", "").dollars // from String extension
// Arithmetic (operators return CurrencyAmount)
val sum = a + c
val diff = c - b
val fee = CurrencyAmount.fromDecimal(Decimal(1))
val total = sum + fee
// Multiply and divide
val tenPercent = Percent.Ten
val discounted = total * tenPercent // percentage of amount
val tripled = total * 3 // Int multiplier
val split = total.divideAndRound(3) // HalfEven to 2 dp
// Multi‑step arithmetic with full precision via Decimal and single coercion at the end
val result = total.mapDecimal { d ->
((d * Decimal(3)) + Decimal(1)) / Decimal(2)
}
// Conversions
val cents: ULong = result.toCents() // 1235uL for 12.35
// Formatting
val s1 = result.formatted(withSymbol = true) // e.g. "$12.35" (platform‑specific)
val s2 = result.formatted(withSymbol = false) // e.g. "12.35"
val s3 = result.formatted(roundedToDollars = true) // e.g. "$12"Additional helpers:
Common API:
fun CurrencyAmount.formatted(
withSymbol: Boolean = true,
roundedToDollars: Boolean = false
): String
Platform notes:
Commands:
./gradlew build
This software is released under the LGPL License. See LICENSE.md for details.
A tiny Kotlin Multiplatform library for working with money amounts in a predictable, base‑10 way.
Currency provides a small, opinionated value type — CurrencyAmount — built on top of the Decimal library (au.lovecraft:decimal). It focuses on common needs for prices and totals: non‑negative amounts, cents precision (2 fractional digits), explicit rounding, arithmetic helpers, and simple platform‑aware formatting.
Note: This is an early, functionally incomplete draft. The API may change.
Configured in this module’s Gradle build:
Kotlin version: 2.2.20
This library is not yet published to a public repository. Recommended ways to consume it today:
Example using an included build:
settings.gradle.kts in your app project:
includeBuild("../currency") // path where this repo lives
Then in your module’s build.gradle.kts:
dependencies {
implementation(project(":currency"))
}
Notes:
// Construct
val a = CurrencyAmount.fromString("12.345")!! // -> 12.35 (HalfEven to 2 dp)
val b = CurrencyAmount.fromCents(1235u) // -> 12.35
val c = 99.dollars // from Int extension
val d = "1_234.00".replace("_", "").dollars // from String extension
// Arithmetic (operators return CurrencyAmount)
val sum = a + c
val diff = c - b
val fee = CurrencyAmount.fromDecimal(Decimal(1))
val total = sum + fee
// Multiply and divide
val tenPercent = Percent.Ten
val discounted = total * tenPercent // percentage of amount
val tripled = total * 3 // Int multiplier
val split = total.divideAndRound(3) // HalfEven to 2 dp
// Multi‑step arithmetic with full precision via Decimal and single coercion at the end
val result = total.mapDecimal { d ->
((d * Decimal(3)) + Decimal(1)) / Decimal(2)
}
// Conversions
val cents: ULong = result.toCents() // 1235uL for 12.35
// Formatting
val s1 = result.formatted(withSymbol = true) // e.g. "$12.35" (platform‑specific)
val s2 = result.formatted(withSymbol = false) // e.g. "12.35"
val s3 = result.formatted(roundedToDollars = true) // e.g. "$12"Additional helpers:
Common API:
fun CurrencyAmount.formatted(
withSymbol: Boolean = true,
roundedToDollars: Boolean = false
): String
Platform notes:
Commands:
./gradlew build
This software is released under the LGPL License. See LICENSE.md for details.