ActionView::Helpers::NumberHelper

number_to_human_size

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

Description

Formats a number of bytes as a string with a unit such as KB or MB.

Arguments

Changing a value updates the ERB and HTML.

Converts the number to a string using ActiveSupport::NumberHelper. The format comes from Rails' built-in English translations (number.human, etc.). A non-numeric value ("abc") is returned as is instead of raising an error. Returns nil for nil. A number below 1024 has its decimal part cut off, without using the rounding options.
options= {}
true raises InvalidNumberError when the number argument is not a number.

Rounding options

2
The number of significant digits, or the number of digits after the decimal point when the :significant option is false.
true treats precision as the number of significant digits. false treats it as the number of digits after the decimal point. When precision is 0, rounds to an integer even if this is true.
true removes trailing 0s after the decimal point. false keeps 0s up to precision digits.
How to round: :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.Used only when the number is 1000 or more after dividing by the unit (since it divides by 1024, when the number is 1000 to 1023, as in 1,020 KB, and beyond ZB). If it is the same as the separator (.), removing trailing zeros also removes the zeros after the delimiter, so the number looks different (1.020 KB becomes 1.02 KB).
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.human.storage_units, 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