PhoenixKit.Utils.Number (phoenix_kit v1.5.1)

View Source

Number formatting utilities for PhoenixKit.

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

Summary

Functions

Formats a number with thousand separators.

Formats a number as a percentage.

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"