0Pricing
Kotlin Multiplatform Academy · レッスン

共有Price・Tax Calculator

金額に関するルールを1つのクロスプラットフォームな場所にまとめます

「共有Price・Tax Calculator」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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 * rate

Add 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) / 100

Support 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 * rate

Both 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. ✅

よくある質問

「共有Price・Tax Calculator」レッスンは無料ですか?

はい。「共有Price・Tax Calculator」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

「共有Price・Tax Calculator」で何を学びますか?

金額に関するルールを1つのクロスプラットフォームな場所にまとめます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Kotlin Multiplatform Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「共有Price・Tax Calculator」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?

はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 共有Price・Tax Calculator
  2. 通貨と数値のフォーマット
  3. 純粋関数としてのビジネスルール
  4. UIを共有コードから分離する
← Kotlin Multiplatform Academyに戻る