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

Commit

Permalink
Added Support For Large Number Formatting(Thousand, Million, Billion)…
Browse files Browse the repository at this point in the history
… in FormatMoneyUseCase (#3634)

* Added Support For Large Number Formatting(Million, Billion) in FormatMoneyUseCase

* Added Support For Large Number Formatting(Million, Billion,thousands) in FormatMoneyUseCase

* Added Support For Large Number Formatting(Million, Billion,thousands) in FormatMoneyUseCase - reviews resolved

* Added Support For Large Number Formatting(Million, Billion,thousands) in FormatMoneyUseCase - refactored FormatMoneyUseCase + updated related unit tests

* Added Support For Large Number Formatting(Million, Billion,thousands) in FormatMoneyUseCase - added shortenAmountFormatter
  • Loading branch information
shamim-emon authored Oct 21, 2024
1 parent a062eca commit c1ca2b9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
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

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 {
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)
}
}
}
60 changes: 55 additions & 5 deletions shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt
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,
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

0 comments on commit c1ca2b9

Please sign in to comment.