From 3694d1b0f5a594ddd9cc257b28bf7db3b1858554 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 1 Aug 2023 21:51:47 -0400 Subject: [PATCH] Avoids using String format. --- .../vitorpamplona/amethyst/ui/note/ReactionsRow.kt | 14 ++++++++++---- .../ui/screen/loggedIn/NotificationScreen.kt | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt index f7cff66a9..bc88e9ae0 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt @@ -112,6 +112,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import java.math.BigDecimal import java.math.RoundingMode +import java.text.DecimalFormat import kotlin.math.roundToInt import kotlin.time.ExperimentalTime import kotlin.time.measureTimedValue @@ -1456,14 +1457,19 @@ val OneGiga = BigDecimal(1000000000) val OneMega = BigDecimal(1000000) val OneKilo = BigDecimal(1000) +var dfG: DecimalFormat = DecimalFormat("#.0G") +var dfM: DecimalFormat = DecimalFormat("#.0M") +var dfK: DecimalFormat = DecimalFormat("#.0k") +var dfN: DecimalFormat = DecimalFormat("#.0") + fun showAmount(amount: BigDecimal?): String { if (amount == null) return "" if (amount.abs() < BigDecimal(0.01)) return "" return when { - amount >= OneGiga -> "%.1fG".format(amount.div(OneGiga).setScale(1, RoundingMode.HALF_UP)) - amount >= OneMega -> "%.1fM".format(amount.div(OneMega).setScale(1, RoundingMode.HALF_UP)) - amount >= OneKilo -> "%.1fk".format(amount.div(OneKilo).setScale(1, RoundingMode.HALF_UP)) - else -> "%.0f".format(amount) + amount >= OneGiga -> dfG.format(amount.div(OneGiga).setScale(1, RoundingMode.HALF_UP)) + amount >= OneMega -> dfM.format(amount.div(OneMega).setScale(1, RoundingMode.HALF_UP)) + amount >= OneKilo -> dfK.format(amount.div(OneKilo).setScale(1, RoundingMode.HALF_UP)) + else -> dfN.format(amount) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt index b34ea0a00..ab2859d6b 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt @@ -58,6 +58,7 @@ import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange import com.vitorpamplona.amethyst.ui.theme.RoyalBlue import java.math.BigDecimal import java.math.RoundingMode +import java.text.DecimalFormat import kotlin.math.roundToInt @Composable @@ -243,14 +244,19 @@ class AmountAxisValueFormatter() : AxisValueFormatter } } +var dfG: DecimalFormat = DecimalFormat("#G") +var dfM: DecimalFormat = DecimalFormat("#M") +var dfK: DecimalFormat = DecimalFormat("#k") +var dfN: DecimalFormat = DecimalFormat("#") + fun showAmountAxis(amount: BigDecimal?): String { if (amount == null) return "" if (amount.abs() < BigDecimal(0.01)) return "" return when { - amount >= OneGiga -> "%.0fG".format(amount.div(OneGiga).setScale(0, RoundingMode.HALF_UP)) - amount >= OneMega -> "%.0fM".format(amount.div(OneMega).setScale(0, RoundingMode.HALF_UP)) - amount >= OneKilo -> "%.0fk".format(amount.div(OneKilo).setScale(0, RoundingMode.HALF_UP)) - else -> "%.0f".format(amount) + amount >= OneGiga -> dfG.format(amount.div(OneGiga).setScale(0, RoundingMode.HALF_UP)) + amount >= OneMega -> dfM.format(amount.div(OneMega).setScale(0, RoundingMode.HALF_UP)) + amount >= OneKilo -> dfK.format(amount.div(OneKilo).setScale(0, RoundingMode.HALF_UP)) + else -> dfN.format(amount) } }