Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/gf_utilities/numeric.rb

Instance Method Summary collapse

Instance Method Details

#with_separator(separator = ',', length = 3) ⇒ Object

ritorna una stringa con le migliaia serparate da <separator> es.: 100000 -> 1.000.000

copiata da wiki.rubygarden.org/Ruby/page/show/FixNumFormat



9
10
11
12
13
14
15
16
# File 'lib/gf_utilities/numeric.rb', line 9

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
  return str
end