0Pricing
Kotlin Multiplatform Academy · Lesson

Format Currency & Numbers

Shared formatting that reads correctly on both apps.

Format Currency & Numbers is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 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.

Computing vs Displaying

Calculating a total is one job; formatting it as text is another. Keeping them separate keeps your shared code clean and reusable.

Why Format in Shared Code

If both apps show prices the same way, users trust them more. A shared formatter guarantees consistent output everywhere.

Two Decimals, Always

Money usually needs exactly two decimals. Decide that rule once so prices never show as 9.5 or 9.512 by accident. 💵

A Portable Pad Trick

You can build a price string with simple math instead of platform APIs. This pad step ensures the cents are always two digits.

fun cents(v: Double): String {
  val c = (v * 100).toInt() % 100
  return c.toString().padStart(2, '0')
}

Assemble the Price String

Combine the whole part and the padded cents to get clean output. This format runs identically on Android and iOS.

fun money(v: Double) = (v.toInt()).toString() + "." + cents(v)

Add the Currency Symbol

Prefix a symbol so the value reads as money. Pass it in rather than hardcoding, so different markets stay flexible.

fun price(v: Double, symbol: String = "$") = symbol + money(v)

Thousands Separators

Big numbers are easier to read grouped, like 1,200. Put this grouping logic in shared code so both apps match.

Locale Is Tricky

Some regions use a comma for decimals. Full locale formatting often needs expect/actual to reach each platform's native formatter.

Declare a Shared Contract

Use expect to declare locale formatting in common code, then let each platform supply its real implementation.

expect fun formatLocalized(amount: Double, currency: String): String

Percentages Too

Tax rates read better as percent. A small percent helper turns 0.18 into a friendly 18% for the UI.

fun percent(rate: Double) = (rate * 100).toInt().toString() + "%"

Format Last, Compute First

Always do the math on numbers, then format only at the edge. This keeps your calculations precise and testable.

Quick Check

Why keep formatting separate from calculation in shared code?

Recap: Numbers In, Strings Out

You formatted money with padded cents, symbols and percents, and reached for expect/actual when locale rules got real. ✅

Frequently asked questions

Is the “Format Currency & Numbers” lesson free?

Yes — the full text of “Format Currency & Numbers” 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 “Format Currency & Numbers”?

Shared formatting that reads correctly on both apps. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Format Currency & Numbers” 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

  1. A Shared Price & Tax Calculator
  2. Format Currency & Numbers
  3. Business Rules as Pure Functions
  4. Keep UI Out of Shared Code
← Back to Kotlin Multiplatform Academy