Skip to content

Commit

Permalink
ASAA-122 - Moved Double.toCurrency from ignite to library
Browse files Browse the repository at this point in the history
  • Loading branch information
br-Emery committed Dec 7, 2023
1 parent 1006a12 commit ac1d538
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kmp-launchpad-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ kotlin {
implementation(compose.ui)
}
commonTest.dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation(libs.kotlin.test)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit ac1d538

Please sign in to comment.