View Source BetterNumber.Delimit (better_number v1.0.1)

Provides functions to delimit numbers into strings.

Summary

Functions

Link to this function

number_to_delimited(number, options \\ %{delimiter: ",", precision: 2, separator: ".", trim_zero_fraction: false})

View Source
@spec number_to_delimited(nil, any()) :: nil
@spec number_to_delimited(BetterNumber.t(), Keyword.t() | map()) :: String.t()

Formats a number into a string with grouped thousands using delimiter.

Parameters

  • number - A float or integer to convert.

  • options - A keyword list of options. See the documentation of all available options below for more information.

Options

  • :precision - The number of decimal places to include. Default: 2

  • :delimiter - The character to use to delimit the number by thousands. Default: ","

  • :separator - The character to use to separate the number from the decimal places. Default: "."

  • :trim_zero_fraction - Whether to trim the zeroes in fraction part of number. Default: "false"

Examples

iex> number_to_delimited(nil)
nil

iex> number_to_delimited(998.999)
"999.00"

iex> number_to_delimited(-234234.234)
"-234,234.23"

iex> number_to_delimited("998.999")
"999.00"

iex> number_to_delimited("-234234.234")
"-234,234.23"

iex> number_to_delimited(12345678)
"12,345,678.00"

iex> number_to_delimited(12345678.05)
"12,345,678.05"

iex> number_to_delimited(12345678, delimiter: ".")
"12.345.678.00"

iex> number_to_delimited(12345678, delimiter: ",")
"12,345,678.00"

iex> number_to_delimited(12345678.05, separator: " ")
"12,345,678 05"

iex> number_to_delimited(98765432.98, delimiter: " ", separator: ",")
"98 765 432,98"

iex> number_to_delimited(Decimal.from_float(9998.2))
"9,998.20"

iex> number_to_delimited "123456789555555555555555555555555"
"123,456,789,555,555,555,555,555,555,555,555.00"

iex> number_to_delimited Decimal.new "123456789555555555555555555555555"
"123,456,789,555,555,555,555,555,555,555,555.00"

iex> number_to_delimited(123456, trim_zero_fraction: true)
"123,456"

iex> number_to_delimited(123456.5, trim_zero_fraction: true)
"123,456.50"