View Source Number.Percentage (number v1.0.5)

Provides functions for converting numbers into percentages.



Formats a number into a percentage string.


Link to this function

number_to_percentage(number, options \\ [])

View Source
@spec number_to_percentage(Number.t(), Keyword.t()) :: String.t()

Formats a number into a percentage string.


  • number - A value to convert. Can be any value that implements Number.Conversion.to_float/1.

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


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

  • :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: "."

Default configuration for these options can be specified in the Number application configuration.

config :number,
  percentage: [
    delimiter: ",",
    separator: ".",
    precision: 2


iex> Number.Percentage.number_to_percentage(nil)

iex> Number.Percentage.number_to_percentage(100)

iex> Number.Percentage.number_to_percentage("98")

iex> Number.Percentage.number_to_percentage(100, precision: 0)

iex> Number.Percentage.number_to_percentage(1000, delimiter: '.', separator: ',')

iex> Number.Percentage.number_to_percentage(302.24398923423, precision: 5)

iex> Number.Percentage.number_to_percentage(Decimal.from_float(59.236), precision: 2)