Number formatter

v0.0.10

v0.0.10StableMobStatelessApache-2.0
Installation mix mishka.ui.gen.mob number_formatter
Source View Source

The Mishka Chelekom Mob Number Formatter renders a number with grouping, decimals, prefix and suffix as text on iOS and Android. It is the native port of the headless Number Formatter and runs on Mob.

Overview of the Mob Number Formatter component for iOS and Android

Number Formatter is mostly arithmetic, so format/2 is a pure function and the node is a Text around its result. You can call the function anywhere you want a string, such as a chart axis, a slider readout or an accessibility label.

Number Formatter keeps no state and sends no events. Pass the number as value, or call format/2 yourself.

format/2 accepts a number, a numeric string or nil. A string is parsed as a number, and a string that does not parse comes back as it is. nil gives an empty string.

Generate the number formatter component

Run this from your Mob app's root to add the component to lib/<app>/components/:

bash
mix mishka.ui.gen.mob number_formatter

The generator registers the <NumberFormatter> tag so you can use it in ~MOB markup. It needs no sibling component and no shared helper module. The examples on this page use the mishka_ prefix of the showcase app (--module-prefix mishka_), so the tag is <MishkaNumberFormatter>. Without a prefix the tag is <NumberFormatter>.

Format a number with a prefix and decimals

Grouping, decimals, prefix and suffix are one pure function. This call groups the integer part, fixes two decimals and adds a dollar prefix.

Format a number with a prefix and decimals

Number formatter card with three formatted numbers: $1,234,567.50 in bold, 1.234,50 € and -98,765 in red, in the MishkaMob showcase app on Android
elixir
MishkaNumberFormatter.format(1234567.5, decimals: 2, prefix: "$")
#=> "$1,234,567.50"

The screenshot renders the same function through three tags, which the next section shows. Grouping applies to the integer part only, and a negative number keeps its sign outside the grouping.

Number formatter as a tag

Write the formatter as a tag when you want a Text node in your markup. The first line renders $1,234,567.50 in semibold weight. The second swaps both separators and adds a suffix to render 1.234,50 €. The third renders -98,765 in the error color.

heex
<MishkaNumberFormatter value={1_234_567.5} decimals={2} prefix="$" font_weight={:semibold} />
<MishkaNumberFormatter
  value={1_234.5}
  decimals={2}
  thousand_separator="."
  decimal_separator=","
  suffix=" €"
/>
<MishkaNumberFormatter value={-98_765} text_color={:error} />

Common formats

Each row is a call to MishkaNumberFormatter.format/2 and the string it returns.

CallResult
format(1234567)"1,234,567"
format(1234.5, decimals: 2)"1,234.50"
format(1234, prefix: "$", suffix: " USD")"$1,234 USD"
format(1234.5, decimals: 2, thousand_separator: ".", decimal_separator: ",")"1.234,50"
format(1234.5678, decimals: 4)"1,234.5678"
format(-1234567)"-1,234,567"
format(1234567, thousand_separator: false)"1234567"

Swap the two separators for European conventions, as the fourth row shows.

Props

PropTypeDefaultDescription
valuenumber0The number.
decimalsintegernilFixed decimal places. nil keeps what the number has.
thousand_separatorstring or boolean","true means ",". false turns grouping off.
decimal_separatorstring"."The radix character.
prefixstringnilWrapped before the digits.
suffixstringnilWrapped after the digits.
text_sizetoken:baseText size.
text_colortoken:on_surfaceText color.
font_weighttoken:regularText weight.

Set the weight with font_weight. Both bridges read it for typography. A bare weight is a layout weight on a Row or Column, so passing it here styles nothing.