Sha256: f8dbca9f48034acf8aeeba65ad43b5a1c94f093dd9e4d7200fcde58c5c543201

Contents?: true

Size: 725 Bytes

Versions: 4

Compression:

Stored size: 725 Bytes

Contents

#          Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

class Numeric
  FILESIZE_FORMAT = [
    ['%.1fT', 1 << 40],
    ['%.1fG', 1 << 30],
    ['%.1fM', 1 << 20],
    ['%.1fK', 1 << 10],
  ]

  # Output this number as easily readable filesize.
  # Usage:
  #   100_000.filesize_format             # => "97.7K"
  #   100_000_000.filesize_format         # => "95.4M"
  #   100_000_000_000.filesize_format     # => "93.1G"
  #   100_000_000_000_000.filesize_format # => "90.9T"
  def filesize_format
    FILESIZE_FORMAT.each do |format, size|
      return format % (self.to_f / size) if self >= size
    end

    self.to_s
  end
end

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
clivecrous-ramaze-0.3.9.5 lib/ramaze/snippets/numeric/filesize_format.rb
manveru-ramaze-2008.07 lib/ramaze/snippets/numeric/filesize_format.rb
manveru-ramaze-2008.08 lib/ramaze/snippets/numeric/filesize_format.rb
ramaze-2008.06 lib/ramaze/snippets/numeric/filesize_format.rb