Amount Formatter

A formatter class that formats a given amount based on configurable options

Import

View-based framework

If you need to reference the AmountFormat component in your Kotlin code, make sure to properly import it on top of your file using the path below.


                                                        
                                                        
                                                            import com.backbase.android.design.amount.AmountFormat
                                                        
                                                            

 

Usage

Basic usage

Create an AmountFormat instance and format amounts with default settings.


                                                        
                                                        
                                                            import java.math.BigDecimal
                                                        // Basic amount formatting
                                                        val formatter = AmountFormat()
                                                        formatter.currencyCode = "USD"
                                                        val formattedAmount = formatter.format(BigDecimal("1234.56"))
                                                        
                                                            

 

Advanced usage

Create an AmountFormat with custom configuration for specific formatting needs.


                                                        
                                                        
                                                            import java.math.BigDecimal
                                                        import java.math.RoundingMode
                                                        import java.util.Locale
                                                        // Advanced formatting with custom options
                                                        val formatter = AmountFormat().apply {
                                                            currencyCode = "EUR"
                                                            enableAbbreviation = true
                                                            enableIsoFormat = true
                                                            enablePositiveSign = true
                                                            enableCustomFractions = true
                                                            minFractionDigits = 2
                                                            maxFractionDigits = 4
                                                            roundMode = RoundingMode.HALF_UP
                                                            locale = Locale("en", "US")
                                                        }
                                                        val formattedAmount = formatter.format(BigDecimal("1200.00"))
                                                        
                                                            

 

Configuration

Properties

Property

Type

Default

currencyCode

String?

null

enableAbbreviation

Boolean

false

enableIsoFormat

Boolean

false

enablePositiveSign

Boolean

false

enableCustomFractions

Boolean

false

locale

Locale

Locale.getDefault()

maxFractionDigits

Int

2

minFractionDigits

Int

2

roundMode

RoundingMode

RoundingMode.DOWN

 

customFormatter

Static property for setting a custom formatter implementation.


                                                        
                                                        
                                                            import java.math.BigDecimal
                                                        // Set custom formatter
                                                        AmountFormat.customFormatter = object : AmountFormat.CustomAmountFormatter {
                                                            override fun format(currencyCode: String?, amount: BigDecimal): String {
                                                                return "Custom: $currencyCode $amount"
                                                            }
                                                        }
                                                        
                                                            

 

currencyCode

Optional three character currency code, as defined in the ISO 4217 standard.


                                                        
                                                        
                                                            // Set currency code
                                                        formatter.currencyCode = "USD"
                                                        formatter.currencyCode = "EUR"
                                                        
                                                            

 

enableAbbreviation

If the amount should be abbreviated.


                                                        
                                                        
                                                            // Enable abbreviation
                                                        formatter.enableAbbreviation = true
                                                        
                                                            

 

enableIsoFormat

If the ISO 4217 currencyCode should be used instead of currency symbol.


                                                        
                                                        
                                                            // Enable ISO format
                                                        formatter.enableIsoFormat = true
                                                        
                                                            

 

enablePositiveSign

If positive amounts should display a plus sign.


                                                        
                                                        
                                                            // Enable positive sign
                                                        formatter.enablePositiveSign = true
                                                        
                                                            

 

enableCustomFractions

If the min/max fractions should be used or the currency's defaultFractionDigits.


                                                        
                                                        
                                                            // Enable custom fractions
                                                        formatter.enableCustomFractions = true
                                                        
                                                            

 

locale

The Locale used for localization of the currency.


                                                        
                                                        
                                                            import java.util.Locale
                                                        // Set specific locale
                                                        formatter.locale = Locale("en", "US")
                                                        formatter.locale = Locale("de", "DE")
                                                        
                                                            

 

maxFractionDigits

The max amount of decimals allowed.


                                                        
                                                        
                                                            // Set maximum fraction digits
                                                        formatter.maxFractionDigits = 4
                                                        
                                                            

 

minFractionDigits

The min amount of decimals allowed.


                                                        
                                                        
                                                            // Set minimum fraction digits
                                                        formatter.minFractionDigits = 2
                                                        
                                                            

 

roundMode

The way the amount will be rounded.


                                                        
                                                        
                                                            import java.math.RoundingMode
                                                        // Set rounding mode
                                                        formatter.roundMode = RoundingMode.DOWN
                                                        formatter.roundMode = RoundingMode.UP
                                                        formatter.roundMode = RoundingMode.HALF_UP
                                                        
                                                            

 

Companion object

Properties

Property

Type

Default

customFormatter

CustomAmountFormatter?

null

 

invoke(currency: String?)

Creates an AmountFormat instance with the specified currency code.


                                                        
                                                        
                                                            import java.math.BigDecimal
                                                        // Create formatter with currency
                                                        val formatter = AmountFormat("USD")
                                                        val formattedAmount = formatter.format(BigDecimal("1234.56"))
                                                        
                                                            

 

Custom formatter

CustomAmountFormatter interface

Interface for providing custom amount formatting logic.


                                                        
                                                        
                                                            import java.math.BigDecimal
                                                        // Implement custom formatter
                                                        val customFormatter = object : AmountFormat.CustomAmountFormatter {
                                                            override fun format(currencyCode: String?, amount: BigDecimal): String {
                                                                val code = currencyCode ?: "USD"
                                                                return "$code $amount"
                                                            }
                                                        }
                                                        // Set custom formatter
                                                        AmountFormat.customFormatter = customFormatter
                                                        
                                                            

 

Events

Custom formatter events

The custom formatter is called when formatting amounts if set.


                                                        
                                                        
                                                            import java.math.BigDecimal
                                                        // Set custom formatter
                                                        AmountFormat.customFormatter = object : AmountFormat.CustomAmountFormatter {
                                                            override fun format(currencyCode: String?, amount: BigDecimal): String {
                                                                // Custom formatting logic
                                                                return "Custom: $currencyCode $amount"
                                                            }
                                                        }
                                                        // Clear custom formatter
                                                        AmountFormat.customFormatter = null
                                                        
                                                            

 

Accessibility

Best practices

  • The formatter automatically handles locale-specific formatting for accessibility
  • Use meaningful currency codes and symbols for screen reader announcements
  • Consider the locale when setting up formatting options for proper accessibility support

                                                        
                                                        
                                                            import java.util.Locale
                                                        // Configure for accessibility
                                                        val formatter = AmountFormat().apply {
                                                            currencyCode = "USD"
                                                            enableIsoFormat = true // More accessible than symbols
                                                            locale = Locale.getDefault()
                                                        }
                                                        
                                                            

 

Design tokens

Tokens used by this component for theming:

Token group amountFormat:

  • DEFAULT_FLAG_ABBREVIATION: Default abbreviation flag
  • DEFAULT_FLAG_CUSTOM_FRACTIONS: Default custom fractions flag
  • DEFAULT_FLAG_ISO_FORMAT: Default ISO format flag
  • DEFAULT_FLAG_POSITIVE_SIGN: Default positive sign flag
  • DEFAULT_FRACTION_DIGITS: Default fraction digits count
  • DEFAULT_ROUNDING_MODE: Default rounding mode

Token group formatting:

  • locale: Locale for currency formatting
  • currencyCode: ISO 4217 currency code
  • fractionDigits: Decimal places configuration
  • roundingMode: Number rounding behavior