number_to_human_size(number, *args) public

Formats the bytes in size into a more understandable representation (e.g., giving it 1500 yields 1.5 KB). This method is useful for reporting file sizes to users. This method returns nil if size cannot be converted into a number. You can customize the format in the options hash.

Options

  • :precision - Sets the level of precision (defaults to 1).
  • :separator - Sets the separator between the units (defaults to ".").
  • :delimiter - Sets the thousands delimiter (defaults to "").

Examples

 number_to_human_size(123)                                          # => 123 Bytes
 number_to_human_size(1234)                                         # => 1.2 KB
 number_to_human_size(12345)                                        # => 12.1 KB
 number_to_human_size(1234567)                                      # => 1.2 MB
 number_to_human_size(1234567890)                                   # => 1.1 GB
 number_to_human_size(1234567890123)                                # => 1.1 TB
 number_to_human_size(1234567, :precision => 2)                     # => 1.18 MB
 number_to_human_size(483989, :precision => 0)                      # => 473 KB
 number_to_human_size(1234567, :precision => 2, :separator => ',')  # => 1,18 MB

You can still use number_to_human_size with the old API that accepts the precision as its optional second parameter:

 number_to_human_size(1234567, 2)    # => 1.18 MB
 number_to_human_size(483989, 0)     # => 473 KB
Show source
Register or log in to add new notes.