number_to_human_size
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (-8)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (6)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (12)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (-38)
- 7.1.3.4 (0)
- What's this?
number_to_human_size(number, options = {})
public
Formats number as bytes into a more human-friendly representation. Useful for reporting file sizes to users.
number_to_human_size(123) # => "123 Bytes" number_to_human_size(1234) # => "1.21 KB" number_to_human_size(12345) # => "12.1 KB" number_to_human_size(1234567) # => "1.18 MB" number_to_human_size(1234567890) # => "1.15 GB" number_to_human_size(1234567890123) # => "1.12 TB" number_to_human_size(1234567890123456) # => "1.1 PB" number_to_human_size(1234567890123456789) # => "1.07 EB"
See #number_to_human if you want to pretty-print a generic number.
Options
- :locale
-
The locale to use for formatting. Defaults to the current locale.
- :precision
-
The level of precision. Defaults to 3.
number_to_human_size(123456, precision: 2) # => "120 KB" number_to_human_size(1234567, precision: 2) # => "1.2 MB"
- :round_mode
-
Specifies how rounding is performed. See BigDecimal.mode. Defaults to :default.
number_to_human_size(123456, precision: 2, round_mode: :up) # => "130 KB"
- :significant
-
Whether :precision should be applied to significant digits instead of fractional digits. Defaults to true.
- :separator
-
The decimal separator. Defaults to ".".
number_to_human_size(1234567, separator: ",") # => "1,18 MB"
- :delimiter
-
The thousands delimiter. Defaults to ",".
- :strip_insignificant_zeros
-
Whether to remove insignificant zeros after the decimal separator. Defaults to true.