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 4 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
26 changes: 22 additions & 4 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 @@ -18,12 +23,25 @@ class FormatMoneyUseCase @Inject constructor(
private val withoutDecimalFormatter = DecimalFormat("###,###", DecimalFormatSymbols(locale))
private val withDecimalFormatter = DecimalFormat("###,###.00", DecimalFormatSymbols(locale))

suspend fun format(value: Double): String {
suspend fun format(value: Double, shortenAmount: Boolean): String {
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
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
}

if (abs(value) >= THOUSAND && shortenAmount) {
val result = if (abs(value) >= BILLION) {
"${formatter.format(value / BILLION)}b"
} else if (abs(value) >= MILLION) {
"${formatter.format(value / MILLION)}m"
} else {
"${formatter.format(value / THOUSAND)}k"
}
return result
} else {
return formatter.format(value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,94 @@ 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 = false,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "13k"
),
ENGLISH_1K_SHORT_AMT_SHOW_DECIMAL(
amount = 13_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "13.00k"
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
),
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 = false,
shortenAmount = true,
locale = Locale.GERMAN,
expectedOutput = "13k"
),
GERMAN_1K_SHORT_AMT_SHOW_DECIMAL(
amount = 13_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.GERMAN,
expectedOutput = "13,00k"
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
),
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 +126,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