BalanceFormatter

@Singleton
class BalanceFormatter @Inject constructor

Utility for formatting token amounts for display.

Responsibilities:

  • Convert raw amounts (BigInteger) to human-readable strings

  • Add thousands separators (1,234,567)

  • Handle decimal places (based on token precision)

  • Append token symbols (e.g., "1,234.56 DUST")

Token Precision:

  • DUST: 15 decimals (1 DUST = 10^15 Specks)

  • NIGHT/TNIGHT: 6 decimals (1 NIGHT = 1,000,000 Stars)

  • Custom tokens: Configurable precision (default 6)

Performance: DecimalFormat is cached to avoid recreating on every format call.

Example Usage:

@Inject lateinit var formatter: BalanceFormatter
formatter.format(BigInteger("1234567890"), "DUST") // "1,234.567890 DUST"
formatter.format(BigInteger.ZERO, "TNIGHT") // "0.000000 TNIGHT"

Constructors

Link copied to clipboard
@Inject
constructor()

Functions

Link copied to clipboard
fun format(amount: BigInteger, tokenType: String, includeSymbol: Boolean = true): String

Format amount with thousands separators and decimals.

Link copied to clipboard
fun formatAbbreviated(amount: BigInteger, tokenType: String): String

Abbreviated whole-token format for tight surfaces (pills, chips, badges).

Link copied to clipboard
fun formatAmount(amount: BigInteger, tokenType: String): String

Format amount without token symbol (for calculations/comparisons).

Link copied to clipboard
fun formatCompact(amount: BigInteger, tokenType: String, includeSymbol: Boolean = true): String

Format for display in lists (shorter format, truncate trailing zeros).