View Source BetterNumber.Percentage (better_number v1.0.1)

Provides functions for converting numbers into percentages.

Summary

Functions

Link to this function

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

View Source
@spec number_to_percentage(BetterNumber.t(), Keyword.t() | map()) :: String.t()

Formats a number into a percentage string.

Parameters

  • number - A value to convert. Can be any value that implements BetterNumber.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: 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> BetterNumber.Percentage.number_to_percentage(100)
"100.00%"

iex> BetterNumber.Percentage.number_to_percentage("98")
"98.00%"

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

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

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

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