Search documentation of phoenix_kit

# PhoenixKit.Utils.Number (phoenix_kit v1.6.19)

View Source

Number formatting utilities for PhoenixKit.

Provides functions for formatting numbers with thousand separators, abbreviations, and other common number formatting needs.

# Summary

## Functions

format(number)

Formats a number with thousand separators.

format_percentage(rate)

Formats a number as a percentage.

format_short(number)

Formats a number with abbreviations (K, M, B).

# Functions

# format(number)

    @spec format(integer() | nil) :: String.t()

Formats a number with thousand separators.

## Examples

    iex> PhoenixKit.Utils.Number.format(1234567)
    "1,234,567"

    iex> PhoenixKit.Utils.Number.format(0)
    "0"

    iex> PhoenixKit.Utils.Number.format(nil)
    "0"

# format_percentage(rate)

    @spec format_percentage(float() | integer() | nil) :: String.t()

Formats a number as a percentage.

## Examples

    iex> PhoenixKit.Utils.Number.format_percentage(95.5)
    "95.5%"

    iex> PhoenixKit.Utils.Number.format_percentage(100)
    "100%"

    iex> PhoenixKit.Utils.Number.format_percentage(nil)
    "0%"

# format_short(number)

    @spec format_short(integer() | nil) :: String.t()

Formats a number with abbreviations (K, M, B).

## Examples

    iex> PhoenixKit.Utils.Number.format_short(1_234_567)
    "1.2M"

    iex> PhoenixKit.Utils.Number.format_short(5_432)
    "5.4K"

    iex> PhoenixKit.Utils.Number.format_short(123)
    "123"