method

number_to_rounded

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

Formats number to a specific level of precision.

number_to_rounded(12345.6789)                # => "12345.679"
number_to_rounded(12345.6789, precision: 2)  # => "12345.68"
number_to_rounded(12345.6789, precision: 0)  # => "12345"
number_to_rounded(12345, precision: 5)       # => "12345.00000"

Options

:locale

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

number_to_rounded(111.234, locale: :fr)
# => "111,234"
:precision

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

number_to_rounded(12345.6789, precision: nil)
# => "12345.6789"
:round_mode

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

number_to_rounded(12.34, precision: 0, round_mode: :up)
# => "13"
:significant

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

number_to_rounded(12345.6789)                                  # => "12345.679"
number_to_rounded(12345.6789, significant: true)               # => "12300"
number_to_rounded(12345.6789, precision: 2)                    # => "12345.68"
number_to_rounded(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.

number_to_rounded(12.34, strip_insignificant_zeros: false)  # => "12.340"
number_to_rounded(12.34, strip_insignificant_zeros: true)   # => "12.34"
number_to_rounded(12.3456, strip_insignificant_zeros: true) # => "12.346"
Show source
Register or log in to add new notes.