A Shared Price & Tax Calculator
Encapsulate money rules in one cross-platform place.
A Shared Price & Tax Calculator is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Money Lives in Shared Code
Price and tax math should live in one shared place so Android and iOS can never disagree on a total. 💰
The Bug You Are Preventing
When each app rounds tax its own way, the same cart shows two prices. A single shared calculator kills that whole class of bug.
Where It Goes
This calculator belongs in commonMain, the source set both platforms compile, so there is exactly one source of truth.
Model the Price First
Start with a tiny data class that holds the amount in plain numbers. Keep it pure Kotlin so it works on every target.
data class Price(val amount: Double, val taxRate: Double)The Tax Formula
Tax is just the amount times the rate. Expressing it as one function means both apps run the exact same line.
fun tax(amount: Double, rate: Double): Double = amount * rateAdd It to the Total
The total is the base amount plus the tax you just computed. Compose small functions instead of one giant one.
fun total(amount: Double, rate: Double): Double = amount + tax(amount, rate)Wrap It in a Calculator
Group the rules in a small class so both apps get a clean, named entry point instead of loose functions.
class PriceCalculator {
fun total(amount: Double, rate: Double) = amount + amount * rate
}Watch the Rounding
Floating point can drift by a cent. Decide your rounding rule once in shared code so totals are identical everywhere.
fun roundMoney(value: Double): Double = kotlin.math.round(value * 100) / 100Support Discounts Too
Real carts have discounts. Apply the discount before tax so the saving feels honest to the customer.
fun discounted(amount: Double, off: Double) = amount * (1 - off)Default Values Help Callers
A sensible default tax rate keeps simple call sites short while still letting either app override it when needed.
fun total(amount: Double, rate: Double = 0.20) = amount + amount * rateBoth Apps Just Call It
Android and iOS each call the same total function. No copied math, no drift, one trusted answer for the price.
val due = PriceCalculator().total(amount = 49.99, rate = 0.18)Quick Check
Where should the price and tax calculator live in a KMP project?
Recap: One Calculator to Rule Both
You modeled price, computed tax, totaled it, and handled rounding in commonMain. One calculator, zero price drift between apps. ✅
Frequently asked questions
Is the “A Shared Price & Tax Calculator” lesson free?
Yes — the full text of “A Shared Price & Tax Calculator” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “A Shared Price & Tax Calculator”?
Encapsulate money rules in one cross-platform place. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A Shared Price & Tax Calculator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- A Shared Price & Tax Calculator
- Format Currency & Numbers
- Business Rules as Pure Functions
- Keep UI Out of Shared Code