method

number_to_percentage

Importance_2
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Class: NumberHelper
number_to_percentage(number, options = {}) public

Formats number as a percentage string.

number_to_percentage(100)   # => "100.000%"
number_to_percentage("99")  # => "99.000%"
number_to_percentage("99x") # => "99x%"

number_to_percentage(12345.6789, delimiter: ".", separator: ",", precision: 2)
# => "12.345,68%"

Options

:locale

The locale to use for formatting. Defaults to the current locale.

number_to_percentage(1000, locale: :fr)
# => "1000,000%"
:precision

The level of precision, or nil to preserve number's precision. Defaults to 2.

number_to_percentage(12.3456789, precision: 4) # => "12.3457%"
number_to_percentage(99.999, precision: 0)     # => "100%"
number_to_percentage(99.999, precision: nil)   # => "99.999%"
:round_mode

Specifies how rounding is performed. See BigDecimal.mode. Defaults to :default.

number_to_percentage(12.3456789, precision: 4, round_mode: :down)
# => "12.3456%"
:significant

Whether :precision should be applied to significant digits instead of fractional digits. Defaults to false.

number_to_percentage(12345.6789)                                  # => "12345.679%"
number_to_percentage(12345.6789, significant: true)               # => "12300%"
number_to_percentage(12345.6789, precision: 2)                    # => "12345.68%"
number_to_percentage(12345.6789, precision: 2, significant: true) # => "12000%"
:separator

The decimal separator. Defaults to ".".

:delimiter

The thousands delimiter. Defaults to ",".

:strip_insignificant_zeros

Whether to remove insignificant zeros after the decimal separator. Defaults to false.

:format

The format of the output. %n represents the number. Defaults to "%n%".

number_to_percentage(100, format: "%n  %")
# => "100.000  %"
Show source
Register or log in to add new notes.