Formattare valute e numeri
Una formattazione condivisa che viene visualizzata correttamente in entrambe le app
Formattare valute e numeri è una lezione Kotlin Multiplatform Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Kotlin Multiplatform Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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): StringPercentages 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. ✅
Domande Frequenti
La lezione «Formattare valute e numeri» è gratuita?
Sì — il testo completo di «Formattare valute e numeri» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Kotlin Multiplatform Academy, passa a CoddyKit PRO. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Cosa imparerò in «Formattare valute e numeri»?
Una formattazione condivisa che viene visualizzata correttamente in entrambe le app Eserciti Kotlin Multiplatform Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Kotlin Multiplatform Academy?
Non è richiesta alcuna esperienza precedente. Kotlin Multiplatform Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Formattare valute e numeri»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Kotlin Multiplatform Academy?
Sì. Ogni lezione Kotlin Multiplatform Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Un calcolatore condiviso di prezzi e imposte
- Formattare valute e numeri
- Regole di business come funzioni pure
- Tenere l'interfaccia fuori dal codice condiviso