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,thousands)…
Browse files Browse the repository at this point in the history
… in FormatMoneyUseCase - reviews resolved
  • Loading branch information
shamim-emon committed Oct 20, 2024
1 parent 4cd1687 commit 668e5f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.math.abs

const val MILLION = 1_000_000
const val BILLION = 1_000_000_000
const val HUNDREDOFTHOUSAND = 100000
const val THOUSAND = 1_000

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

suspend fun format(value: Double, shortenAmount: Boolean): String {
when (abs(value) >= HUNDREDOFTHOUSAND && shortenAmount) {
when (abs(value) >= THOUSAND && shortenAmount) {
true -> {
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, "%.2fk", value / HUNDREDOFTHOUSAND)
String.format(locale, "%.2fk", value / THOUSAND)
}
return result
}
Expand Down
28 changes: 14 additions & 14 deletions shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,70 @@ class FormatMoneyUseCaseTest {
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_100K_SHORT_AMT(
amount = 130000.10,
ENGLISH_1K_SHORT_AMT(
amount = 13_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "1.30k"
expectedOutput = "13.00k"
),
ENGLISH_MILLION_SHORT_AMT(
amount = 1230000.10,
amount = 1_230_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "1.23m"
),
ENGLISH_BILLION_SHORT_AMT(
amount = 1233000000.10,
amount = 1_233_000_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.ENGLISH,
expectedOutput = "1.23b"
),
GERMAN_100K_SHORT_AMT(
amount = 130000.10,
GERMAN_1K_SHORT_AMT(
amount = 13_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.GERMAN,
expectedOutput = "1,30k"
expectedOutput = "13,00k"
),
GERMAN_MILLION_SHORT_AMT(
amount = 1230000.10,
amount = 1_230_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.GERMAN,
expectedOutput = "1,23m"
),
GERMAN_BILLION_SHORT_AMT(
amount = 1233000000.10,
amount = 1_233_000_000.10,
showDecimal = true,
shortenAmount = true,
locale = Locale.GERMAN,
Expand Down

0 comments on commit 668e5f9

Please sign in to comment.