diff --git a/shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt b/shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt index 076b4c625a..5255a54843 100644 --- a/shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt +++ b/shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt @@ -7,9 +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 MILLION = 1_000_000 const val BILLION = 1_000_000_000 +const val HUNDREDOFTHOUSAND = 100000 class FormatMoneyUseCase @Inject constructor( private val features: Features, @@ -21,13 +23,15 @@ class FormatMoneyUseCase @Inject constructor( private val withoutDecimalFormatter = DecimalFormat("###,###", DecimalFormatSymbols(locale)) private val withDecimalFormatter = DecimalFormat("###,###.00", DecimalFormatSymbols(locale)) - suspend fun format(value: Double): String { - when (value >= MILLION) { + suspend fun format(value: Double, shortenAmount: Boolean): String { + when (abs(value) >= HUNDREDOFTHOUSAND && shortenAmount) { true -> { - val result = if (value >= BILLION) { - String.format(locale, "%.2fB", value / BILLION) + val result = if (abs(value) >= BILLION) { + String.format(locale, "%.2fb", value / BILLION) + } else if (abs(value) >= MILLION) { + String.format(locale, "%.2fm", value / MILLION) } else { - String.format(locale, "%.2fM", value / MILLION) + String.format(locale, "%.2fk", value / HUNDREDOFTHOUSAND) } return result } diff --git a/shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt b/shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt index 2ab16d219b..776548f345 100644 --- a/shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt +++ b/shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt @@ -23,56 +23,79 @@ 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, showDecimal = true, + shortenAmount = false, locale = Locale.ENGLISH, expectedOutput = "1,000.12" ), ENG_HIDE_DECIMAL( amount = 1000.12, showDecimal = false, + shortenAmount = false, locale = Locale.ENGLISH, expectedOutput = "1,000" ), GERMAN_SHOW_DECIMAL( amount = 1000.12, showDecimal = true, + shortenAmount = false, locale = Locale.GERMAN, expectedOutput = "1.000,12" ), GERMAN_HIDE_DECIMAL( amount = 1000.12, showDecimal = false, + shortenAmount = false, locale = Locale.GERMAN, expectedOutput = "1.000" ), - ENGLISH_MILLION( + ENGLISH_100K_SHORT_AMT( + amount = 130000.10, + showDecimal = true, + shortenAmount = true, + locale = Locale.ENGLISH, + expectedOutput = "1.30k" + ), + ENGLISH_MILLION_SHORT_AMT( amount = 1230000.10, showDecimal = true, + shortenAmount = true, locale = Locale.ENGLISH, - expectedOutput = "1.23M" + expectedOutput = "1.23m" ), - ENGLISH_BILLION( + ENGLISH_BILLION_SHORT_AMT( amount = 1233000000.10, showDecimal = true, + shortenAmount = true, locale = Locale.ENGLISH, - expectedOutput = "1.23B" + expectedOutput = "1.23b" + ), + GERMAN_100K_SHORT_AMT( + amount = 130000.10, + showDecimal = true, + shortenAmount = true, + locale = Locale.GERMAN, + expectedOutput = "1,30k" ), - GERMAN_MILLION( + GERMAN_MILLION_SHORT_AMT( amount = 1230000.10, showDecimal = true, + shortenAmount = true, locale = Locale.GERMAN, - expectedOutput = "1,23M" + expectedOutput = "1,23m" ), - GERMAN_BILLION( + GERMAN_BILLION_SHORT_AMT( amount = 1233000000.10, showDecimal = true, + shortenAmount = true, locale = Locale.GERMAN, - expectedOutput = "1,23B" + expectedOutput = "1,23b" ), } @@ -89,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