-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ASAA-122 - Moved Double.toCurrency from ignite to library
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...ndroidMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/androidUnitTest/kotlin/com/bottlerocketstudios/launchpad/compose/CurrencyUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ose/src/commonMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
17 changes: 17 additions & 0 deletions
17
...se/src/iosMain/kotlin/com/bottlerocketstudios/launchpad/compose/util/CurrencyUtils.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |