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

Added Support For Large Number Formatting(Thousand, Million, Billion) in FormatMoneyUseCase #3634

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import javax.inject.Inject
import kotlin.math.abs

const val THOUSAND = 1_000
const val MILLION = 1_000_000
const val BILLION = 1_000_000_000
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved

class FormatMoneyUseCase @Inject constructor(
private val features: Features,
Expand All @@ -17,13 +22,26 @@ class FormatMoneyUseCase @Inject constructor(
private val locale = devicePreferences.locale()
private val withoutDecimalFormatter = DecimalFormat("###,###", DecimalFormatSymbols(locale))
private val withDecimalFormatter = DecimalFormat("###,###.00", DecimalFormatSymbols(locale))
private val shortenAmountFormatter = DecimalFormat("###,###.##", DecimalFormatSymbols(locale))

suspend fun format(value: Double): String {
val showDecimalPoint = features.showDecimalNumber.isEnabled(context)
suspend fun format(value: Double, shortenAmount: Boolean): String {
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
if (abs(value) >= THOUSAND && shortenAmount) {
val result = if (abs(value) >= BILLION) {
"${shortenAmountFormatter.format(value / BILLION)}b"
} else if (abs(value) >= MILLION) {
"${shortenAmountFormatter.format(value / MILLION)}m"
} else {
"${shortenAmountFormatter.format(value / THOUSAND)}k"
}
return result
} else {
val showDecimalPoint = features.showDecimalNumber.isEnabled(context)

return when (showDecimalPoint) {
true -> withDecimalFormatter.format(value)
false -> withoutDecimalFormatter.format(value)
val formatter = when (showDecimalPoint) {
true -> withDecimalFormatter
false -> withoutDecimalFormatter
}
return formatter.format(value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,80 @@ class FormatMoneyUseCaseTest {
enum class MoneyFormatterTestCase(
val amount: Double,
val showDecimal: Boolean,
val shortenAmount: Boolean,
val locale: Locale,
val expectedOutput: String
) {
ENG_SHOW_DECIMAL(
amount = 1000.12,
amount = 1_000.12,
showDecimal = true,
shortenAmount = false,
locale = Locale.ENGLISH,
expectedOutput = "1,000.12"
),
ENG_HIDE_DECIMAL(
amount = 1000.12,
amount = 1_000.12,
showDecimal = false,
shortenAmount = false,
locale = Locale.ENGLISH,
expectedOutput = "1,000"
),
GERMAN_SHOW_DECIMAL(
amount = 1000.12,
amount = 1_000.12,
showDecimal = true,
shortenAmount = false,
locale = Locale.GERMAN,
expectedOutput = "1.000,12"
),
GERMAN_HIDE_DECIMAL(
amount = 1000.12,
amount = 1_000.12,
showDecimal = false,
shortenAmount = false,
locale = Locale.GERMAN,
expectedOutput = "1.000"
),
ENGLISH_1K_SHORT_AMT(
amount = 13_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "13k"
),
ENGLISH_MILLION_SHORT_AMT(
amount = 1_233_500.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "1.23m"
),
ENGLISH_BILLION_SHORT_AMT(
amount = 1_233_000_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "1.23b"
),
GERMAN_1K_SHORT_AMT(
amount = 13_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.GERMAN,
expectedOutput = "13k"
),
GERMAN_MILLION_SHORT_AMT(
amount = 1_233_500.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.GERMAN,
expectedOutput = "1,23m"
),
GERMAN_BILLION_SHORT_AMT(
amount = 1_233_000_000.10,
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
showDecimal = true,
shortenAmount = true,
locale = Locale.GERMAN,
expectedOutput = "1,23b"
),
}

private lateinit var formatMoneyUseCase: FormatMoneyUseCase
Expand All @@ -65,7 +112,10 @@ class FormatMoneyUseCaseTest {
formatMoneyUseCase = FormatMoneyUseCase(features, devicePreferences, context)

// when
val result = formatMoneyUseCase.format(value = testCase.amount)
val result = formatMoneyUseCase.format(
value = testCase.amount,
shortenAmount = testCase.shortenAmount
)

// then
result shouldBe testCase.expectedOutput
Expand Down