Sha256: 57f1d3c4b86b16408b6331825465c2497a1356e0d39d6a5a332860b6f2ca48f8

Contents?: true

Size: 677 Bytes

Versions: 9

Compression:

Stored size: 677 Bytes

Contents

# -*- coding: utf-8 -*-
class Numeric
  #
  # returns a string separated by the thousands <separator>
  # es.: 100000 -> 1.000.000
  #
  def with_separator(separator = ',', length = 3)
    splitter      = Regexp.compile "(\\d{#{length}})"
    before, after = self.to_s.split('.')
    before        = before.reverse.gsub splitter, '\1' + separator
    str           = "#{ before.chomp(separator).reverse }"
    str += ".#{ after }" if after
    str
  end

  def to_human
    if self == 0
      return "0B"
    end 
    units = %w{B KB MB GB TB}
   
    e     = (Math.log(self)/Math.log(1024)).floor
    s     = "%.3f" % (to_f / 1024**e)
    s.sub(/\.?0*$/, units[e])
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
tree.rb-0.3.11 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.10 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.9 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.8 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.7 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.6 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.5 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.4 lib/tree_rb/extension_numeric.rb
tree.rb-0.3.3 lib/tree_rb/extension_numeric.rb