expect and actual

So far, so good. But it would be nicer if Doubloons4Gold used currency formatting, so your output looked like this:

    Hello from Kotlin/JVM!
    The current exchange rate is $1.38 per doubloon
    How many doubloons do you want?
    50
    50.0 doubloons will cost you $69.04

You want this formatting to apply across all platforms your application supports, so it belongs in your common codebase. Create a new file in commonMain/kotlin called Currency.kt and define an extension function on Double called formatAsCurrency.

As the name suggests, this function will convert a double to an appropriately formatted string for the user’s local currency. But for now, leave the implementation as a to-do. We will explain how to implement it shortly. ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.