Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Updated FormatMoneyUseCase - reviews resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
shamim-emon committed Nov 2, 2024
1 parent 6900c68 commit 1db738f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 41 deletions.
40 changes: 3 additions & 37 deletions shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class FormatMoneyUseCase @Inject constructor(
private val withoutDecimalFormatter = DecimalFormat("###,###", DecimalFormatSymbols(locale))
private val withDecimalFormatter = DecimalFormat("###,###.00", DecimalFormatSymbols(locale))
private val shortenAmountFormatter = DecimalFormat("###,###.##", DecimalFormatSymbols(locale))
private val cryptoFormatter =
DecimalFormat("###,###,##0.${"0".repeat(9)}", DecimalFormatSymbols(locale))
private val cryptoFormatter = DecimalFormat("#".repeat(9), DecimalFormatSymbols(locale))

/**
* Formats a currency or cryptocurrency amount based on the input parameters.
Expand All @@ -44,8 +43,8 @@ class FormatMoneyUseCase @Inject constructor(
* @return The formatted string representation of the value.
*/
suspend fun format(value: Double, shortenAmount: Boolean, isCrypto: Boolean = false): String {
val result = if (isCrypto) {
formatCrypto(value)
return if (isCrypto) {
cryptoFormatter.format(value)
} else if (abs(value) >= THOUSAND && shortenAmount) {
if (abs(value) >= BILLION) {
"${shortenAmountFormatter.format(value / BILLION)}b"
Expand All @@ -63,38 +62,5 @@ class FormatMoneyUseCase @Inject constructor(
}
formatter.format(value)
}

return result
}

/**
* Formats a cryptocurrency value with up to 9 decimal places, removing unnecessary trailing zeros.
*
* @param value The cryptocurrency value to format.
* @return The formatted cryptocurrency value as a string.
*/
private fun formatCrypto(value: Double): String {
val result = cryptoFormatter.format(value)
return when {
result.lastOrNull() == localDecimalSeparator().firstOrNull() -> {
val newResult = result.dropLast(1)
newResult.ifEmpty { "0" }
}

result.isEmpty() -> {
"0"
}

else -> result
}
}

/**
* Retrieves the local decimal separator based on the user's locale.
*
* @return The decimal separator as a string.
*/
private fun localDecimalSeparator(): String {
return DecimalFormatSymbols(locale).decimalSeparator.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,31 @@ class FormatMoneyUseCaseTest {
shortenAmount = false,
isCrypto = true,
locale = Locale.ENGLISH,
expectedOutput = "123,456.000000000"
expectedOutput = "123456"
),
ENG_HIDE_DECIMAL_CRYPTO(
amount = 123_456.0,
showDecimal = false,
shortenAmount = false,
isCrypto = true,
locale = Locale.ENGLISH,
expectedOutput = "123,456.000000000"
expectedOutput = "123456"
),
GERMAN_SHOW_DECIMAL_CRYPTO(
amount = 123_456.0,
showDecimal = true,
shortenAmount = false,
isCrypto = true,
locale = Locale.GERMAN,
expectedOutput = "123.456,000000000"
expectedOutput = "123456"
),
GERMAN_HIDE_DECIMAL_CRYPTO(
amount = 123_456.0,
showDecimal = false,
shortenAmount = false,
isCrypto = true,
locale = Locale.GERMAN,
expectedOutput = "123.456,000000000"
expectedOutput = "123456"
),
}

Expand Down

0 comments on commit 1db738f

Please sign in to comment.