0Pricing
Kotlin Multiplatform Academy · บทเรียน

จัดรูปแบบสกุลเงินและตัวเลข

การจัดรูปแบบแบบ shared ที่แสดงผลถูกต้องในทั้งสองแอป

จัดรูปแบบสกุลเงินและตัวเลข เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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): 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. ✅

คำถามที่พบบ่อย

บทเรียน “จัดรูปแบบสกุลเงินและตัวเลข” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “จัดรูปแบบสกุลเงินและตัวเลข” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “จัดรูปแบบสกุลเงินและตัวเลข”

การจัดรูปแบบแบบ shared ที่แสดงผลถูกต้องในทั้งสองแอป คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “จัดรูปแบบสกุลเงินและตัวเลข” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เครื่องคำนวณราคาและภาษีแบบ shared
  2. จัดรูปแบบสกุลเงินและตัวเลข
  3. กฎทางธุรกิจในรูปฟังก์ชันบริสุทธิ์
  4. ไม่นำ UI ไว้ในโค้ด shared
← กลับไปที่ Kotlin Multiplatform Academy