ActionView::Helpers::NumberHelper

number_with_precision

API docs (Rails 8.1.4.1) ↗
def number_with_precision(number, options = {})

Description

Formats the number rounded to a given number of digits.

Arguments

Changing a value updates the ERB and HTML.

Converts the number to a string using ActiveSupport::NumberHelper. A non-numeric value ("abc") is returned as is instead of raising an error. Returns nil for nil.
options= {}
true raises InvalidNumberError when the number argument is not a number.

Rounding options

2
The number of digits after the decimal point, or the number of significant digits when the :significant option is true.
true treats the :precision option as the number of significant digits. When precision is 0, rounds to an integer even if this is true.
true removes trailing 0 digits after the decimal point.
The rounding mode. :down rounds toward zero and :up rounds away from zero. Defaults to rounding half up. Other BigDecimal rounding modes (:half_even, :half_down, :ceiling, :floor, etc.) also work.
","
The string used for the decimal point. It is escaped.
"."
The string inserted every three digits. It is escaped.
Every NumberHelper helper handles the rounding options the same way (ActiveSupport::NumberHelper's NumberToRoundedConverter). Their defaults differ from helper to helper.Options not listed here do not become HTML attributes. With :locale, uses that language's number format translations (number.precision.format, etc.). This site provides no number format translations, so :ja gets Rails' built-in English format (a language this site does not have raises I18n::InvalidLocale).

ERB

Changes from the previous output: Added Removed Changed


          

HTML