Skip to content

Commit

Permalink
Feat: Add currency formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakhar-Agarwal-byte committed Mar 8, 2023
1 parent 8e29ce9 commit 6c93389
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ fun BalanceBox(
CurrencyToggleText(currencyToggleState = currencyToggleState, text = CurrencyType.SATS)
}
}
val balanceDisplay: String = if (currencyToggleState.value) balance.toString() else balance.formatInBtc()
var balanceDisplay: String = if (currencyToggleState.value) balance.toString() else balance.formatInBtc()
balanceDisplay = formatCurrency(balanceDisplay)
val currencyDisplay: String = if (currencyToggleState.value) CurrencyType.SATS.toString().lowercase() else CurrencyType.BTC.toString().lowercase()
Text(
text = balanceDisplay,
Expand Down Expand Up @@ -217,6 +218,18 @@ fun BalanceBox(
}
}

fun formatCurrency(amount: String): String {
val regex = "(\\d)(?=(\\d{3})+\$)".toRegex()
val dotIdx = amount.indexOf('.')
return if (dotIdx == -1) {
amount.replace(regex, "\$1,")
} else {
val num = amount.substring(0, dotIdx).replace(regex, "\$1,")
val dec = amount.substring(dotIdx+1).replace(regex, "\$1'")
"$num.$dec"
}
}

@Composable
fun SendReceive(navController: NavHostController) {
Row(
Expand Down

0 comments on commit 6c93389

Please sign in to comment.