Class: Numeric
- Inherits:
-
Object
- Object
- Numeric
- Defined in:
- lib/tree_rb/extension_numeric.rb
Instance Method Summary collapse
- #to_human ⇒ Object
-
#with_separator(separator = ',', length = 3) ⇒ Object
returns a string separated by the thousands <separator> es.: 100000 -> 1.000.000.
Instance Method Details
#to_human ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tree_rb/extension_numeric.rb', line 16 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 |
#with_separator(separator = ',', length = 3) ⇒ Object
returns a string separated by the thousands <separator> es.: 100000 -> 1.000.000
7 8 9 10 11 12 13 14 |
# File 'lib/tree_rb/extension_numeric.rb', line 7 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 |