From ac1d538237f5e96e473d502da905c266ef5c9dc6 Mon Sep 17 00:00:00 2001 From: Eric Emery Date: Thu, 7 Dec 2023 14:04:11 -0600 Subject: [PATCH] ASAA-122 - Moved Double.toCurrency from ignite to library --- kmp-launchpad-compose/build.gradle.kts | 2 ++ .../compose/util/CurrencyUtils.android.kt | 11 ++++++++++ .../launchpad/compose/CurrencyUtilsTest.kt | 21 +++++++++++++++++++ .../launchpad/compose/util/CurrencyUtils.kt | 15 +++++++++++++ .../compose/util/CurrencyUtils.ios.kt | 17 +++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 kmp-launchpad-compose/src/androidMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.android.kt create mode 100644 kmp-launchpad-compose/src/androidUnitTest/kotlin/com/bottlerocketstudios/launchpad/compose/CurrencyUtilsTest.kt create mode 100644 kmp-launchpad-compose/src/commonMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.kt create mode 100644 kmp-launchpad-compose/src/iosMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.ios.kt diff --git a/kmp-launchpad-compose/build.gradle.kts b/kmp-launchpad-compose/build.gradle.kts index 26651f7..19ad2f6 100644 --- a/kmp-launchpad-compose/build.gradle.kts +++ b/kmp-launchpad-compose/build.gradle.kts @@ -45,6 +45,8 @@ kotlin { implementation(compose.ui) } commonTest.dependencies { + implementation(kotlin("test-common")) + implementation(kotlin("test-annotations-common")) implementation(libs.kotlin.test) } } diff --git a/kmp-launchpad-compose/src/androidMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.android.kt b/kmp-launchpad-compose/src/androidMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.android.kt new file mode 100644 index 0000000..f3354ec --- /dev/null +++ b/kmp-launchpad-compose/src/androidMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.android.kt @@ -0,0 +1,11 @@ +package com.bottlerocketstudios.launchpad.compose.util + +import java.text.NumberFormat +import java.util.Currency +import java.util.Locale + +actual fun Double?.toCurrency(): String { + val numberFormat = NumberFormat.getCurrencyInstance() + numberFormat.currency = Currency.getInstance(Locale.getDefault()) + return numberFormat.format(this ?: 0.0) +} diff --git a/kmp-launchpad-compose/src/androidUnitTest/kotlin/com/bottlerocketstudios/launchpad/compose/CurrencyUtilsTest.kt b/kmp-launchpad-compose/src/androidUnitTest/kotlin/com/bottlerocketstudios/launchpad/compose/CurrencyUtilsTest.kt new file mode 100644 index 0000000..8a6492c --- /dev/null +++ b/kmp-launchpad-compose/src/androidUnitTest/kotlin/com/bottlerocketstudios/launchpad/compose/CurrencyUtilsTest.kt @@ -0,0 +1,21 @@ +package com.bottlerocketstudios.launchpad.compose + +import com.bottlerocketstudios.launchpad.compose.util.toCurrency +import java.util.Locale +import kotlin.test.Test +import kotlin.test.assertEquals + + +class CurrencyUtilsTest { + + @Test + fun `test double toCurrency`() { + Locale.setDefault(Locale.US) + + val input = 1234.56 + val expectedOutput = "$1,234.56" + val actualOutput = input.toCurrency() + + assertEquals(expectedOutput, actualOutput) + } +} \ No newline at end of file diff --git a/kmp-launchpad-compose/src/commonMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.kt b/kmp-launchpad-compose/src/commonMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.kt new file mode 100644 index 0000000..73659a3 --- /dev/null +++ b/kmp-launchpad-compose/src/commonMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.kt @@ -0,0 +1,15 @@ +package com.bottlerocketstudios.launchpad.compose.util + + +/** + * Kotlin Multiplatform function - convertToCurrencyString + * + * This function takes a Double value representing a raw amount of currency, + * and returns a String representing that amount formatted appropriately as + * currency for the current locale of the device running the code. + * + * @param [amount] The raw amount of currency as a Double + * @return A string representing that amount in the appropriate currency + * format for the device's current locale. + */ +expect fun Double?.toCurrency(): String diff --git a/kmp-launchpad-compose/src/iosMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.ios.kt b/kmp-launchpad-compose/src/iosMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.ios.kt new file mode 100644 index 0000000..14fbbe7 --- /dev/null +++ b/kmp-launchpad-compose/src/iosMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.ios.kt @@ -0,0 +1,17 @@ +package com.bottlerocketstudios.launchpad.compose.util + +import platform.Foundation.NSLocale +import platform.Foundation.NSNumber +import platform.Foundation.NSNumberFormatter +import platform.Foundation.NSNumberFormatterCurrencyStyle +import platform.Foundation.currentLocale +import platform.Foundation.numberWithDouble + +actual fun Double?.toCurrency(): String { + val formatter = NSNumberFormatter() + formatter.numberStyle = NSNumberFormatterCurrencyStyle + formatter.locale = NSLocale.currentLocale() + + val number = NSNumber.numberWithDouble(this ?: 0.0) + return formatter.stringFromNumber(number) ?: "NaN" +}