number v1.0.1 Number.Percentage

Provides functions for converting numbers into percentages.

Link to this section Summary

Functions

Formats a number into a percentage string.

Link to this section Functions

Link to this function

number_to_percentage(number, options \\ [])

number_to_percentage(number(), Keyword.t()) :: String.t()

Formats a number into a percentage string.

Parameters

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

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
                ]

Examples

iex> Number.Percentage.number_to_percentage(100)
"100.000%"

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

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

iex> Number.Percentage.number_to_percentage(1000, delimiter: '.', separator: ',')
"1.000,000%"

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

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