Skip to content

Commit af26ec2

Browse files
committed
refactor: formatMoney method
1 parent 289f2c1 commit af26ec2

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

app/src/main/java/to/bitkit/services/CurrencyService.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import to.bitkit.async.ServiceQueue
55
import to.bitkit.data.BlocktankHttpClient
66
import to.bitkit.models.ConvertedAmount
77
import to.bitkit.models.FxRate
8+
import to.bitkit.ui.utils.formatCurrency
89
import to.bitkit.utils.AppError
910
import java.math.BigDecimal
1011
import java.math.RoundingMode
11-
import java.text.DecimalFormat
12-
import java.text.DecimalFormatSymbols
13-
import java.util.Locale
1412
import javax.inject.Inject
1513
import javax.inject.Singleton
1614
import kotlin.math.pow
@@ -57,16 +55,7 @@ class CurrencyService @Inject constructor(
5755
val btcAmount = BigDecimal(sats).divide(BigDecimal(100_000_000))
5856
val value: BigDecimal = btcAmount.multiply(BigDecimal.valueOf(rate.rate))
5957

60-
val symbols = DecimalFormatSymbols(Locale.getDefault()).apply {
61-
decimalSeparator = '.'
62-
groupingSeparator = ','
63-
}
64-
val formatter = DecimalFormat("#,##0.00", symbols).apply {
65-
minimumFractionDigits = 2
66-
maximumFractionDigits = 2
67-
}
68-
69-
val formatted = runCatching { formatter.format(value) }.getOrNull() ?: return null
58+
val formatted = value.formatCurrency() ?: return null
7059

7160
return ConvertedAmount(
7261
value = value,

app/src/main/java/to/bitkit/ui/utils/Text.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import androidx.compose.ui.text.font.FontWeight
1313
import androidx.compose.ui.text.fromHtml
1414
import androidx.compose.ui.text.withStyle
1515
import to.bitkit.ui.theme.Colors
16+
import java.math.BigDecimal
17+
import java.text.DecimalFormat
18+
import java.text.DecimalFormatSymbols
19+
import java.util.Locale
1620

1721
fun String.withAccent(
1822
defaultColor: Color = Color.Unspecified,
@@ -122,3 +126,22 @@ fun localizedRandom(@StringRes id: Int): String {
122126
}
123127
}
124128
}
129+
130+
fun BigDecimal.formatCurrency() : String? {
131+
val symbols = DecimalFormatSymbols(Locale.getDefault()).apply {
132+
decimalSeparator = '.'
133+
groupingSeparator = ','
134+
}
135+
val formatter = DecimalFormat("#,##0.00", symbols).apply {
136+
minimumFractionDigits = 2
137+
maximumFractionDigits = 2
138+
}
139+
140+
return runCatching { formatter.format(this) }.getOrNull()
141+
}
142+
143+
fun Double.formatCurrency() : String? {
144+
return BigDecimal(this).formatCurrency()
145+
}
146+
147+

0 commit comments

Comments
 (0)