Una calculadora compartida de precios e impuestos
Encapsule las reglas monetarias en un único lugar multiplataforma
Una calculadora compartida de precios e impuestos es una lección gratuita de Kotlin Multiplatform Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Kotlin Multiplatform Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. ✅
Preguntas frecuentes
¿La lección «Una calculadora compartida de precios e impuestos» es gratis?
Sí — el texto completo de «Una calculadora compartida de precios e impuestos» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Kotlin Multiplatform Academy, actualiza a CoddyKit PRO. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Una calculadora compartida de precios e impuestos»?
Encapsule las reglas monetarias en un único lugar multiplataforma Practicas Kotlin Multiplatform Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Kotlin Multiplatform Academy?
No se requiere experiencia previa. Kotlin Multiplatform Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Una calculadora compartida de precios e impuestos»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Kotlin Multiplatform Academy?
Sí. Cada lección de Kotlin Multiplatform Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Una calculadora compartida de precios e impuestos
- Formato de divisas y números
- Reglas de negocio como funciones puras
- Mantenga la interfaz fuera del código compartido