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(Million, Billion) in Format…
Browse files Browse the repository at this point in the history
…MoneyUseCase
  • Loading branch information
shamim-emon committed Oct 20, 2024
1 parent 70eb664 commit a0ff1a8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
24 changes: 20 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 @@ -8,6 +8,9 @@ import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import javax.inject.Inject

const val MILLION = 1_000_000
const val BILLION = 1_000_000_000

class FormatMoneyUseCase @Inject constructor(
private val features: Features,
private val devicePreferences: DevicePreferences,
Expand All @@ -19,11 +22,24 @@ class FormatMoneyUseCase @Inject constructor(
private val withDecimalFormatter = DecimalFormat("###,###.00", DecimalFormatSymbols(locale))

suspend fun format(value: Double): String {
val showDecimalPoint = features.showDecimalNumber.isEnabled(context)
when (value >= MILLION) {
true -> {
val result = if (value >= BILLION) {
String.format(locale, "%.2fB", value / BILLION)
} else {
String.format(locale, "%.2fM", value / MILLION)
}
return result
}

else -> {
val showDecimalPoint = features.showDecimalNumber.isEnabled(context)

return when (showDecimalPoint) {
true -> withDecimalFormatter.format(value)
false -> withoutDecimalFormatter.format(value)
return when (showDecimalPoint) {
true -> withDecimalFormatter.format(value)
false -> withoutDecimalFormatter.format(value)
}
}
}
}
}
24 changes: 24 additions & 0 deletions shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ class FormatMoneyUseCaseTest {
locale = Locale.GERMAN,
expectedOutput = "1.000"
),
ENGLISH_MILLION(
amount = 1230000.10,
showDecimal = true,
locale = Locale.ENGLISH,
expectedOutput = "1.23M"
),
ENGLISH_BILLION(
amount = 1233000000.10,
showDecimal = true,
locale = Locale.ENGLISH,
expectedOutput = "1.23B"
),
GERMAN_MILLION(
amount = 1230000.10,
showDecimal = true,
locale = Locale.GERMAN,
expectedOutput = "1,23M"
),
GERMAN_BILLION(
amount = 1233000000.10,
showDecimal = true,
locale = Locale.GERMAN,
expectedOutput = "1,23B"
),
}

private lateinit var formatMoneyUseCase: FormatMoneyUseCase
Expand Down

0 comments on commit a0ff1a8

Please sign in to comment.