تنسيق العملات والأرقام
تنسيق مشترك يُعرض بشكل صحيح في كلا التطبيقين
تنسيق العملات والأرقام درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. ✅
الأسئلة الشائعة
هل درس «تنسيق العملات والأرقام» مجاني؟
نعم — نص درس «تنسيق العملات والأرقام» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «تنسيق العملات والأرقام»؟
تنسيق مشترك يُعرض بشكل صحيح في كلا التطبيقين تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «تنسيق العملات والأرقام»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- حاسبة مشتركة للأسعار والضرائب
- تنسيق العملات والأرقام
- قواعد الأعمال كدوال صِرفة
- إبعاد الواجهة عن التعليمات البرمجية المشتركة