Module: Numerology

Defined in:
lib/numerology.rb,
lib/numerology/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#format_like(example) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/numerology.rb', line 4

def format_like(example)
  if example.gsub(/\D/,'') =~ /(0+$)/
    sig_fig = $1.length
    temp = self.round(sig_fig*-1)
  else
    temp = self
  end

  if example =~ /^[0\s]/
    formatted_string = "%#{example[0]}#{example.gsub(/\D/,'').length}d" % temp 
  else
    formatted_string = temp.to_s
  end

  formatted_string = separate_thousands(formatted_string, example[-4]) if example =~ /(\D)\d{3}$/

  formatted_string
end

#separate_thousands(formatted_string, separator) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/numerology.rb', line 23

def separate_thousands(formatted_string, separator)
  formatted_string =~ /([^\.]*)(\..*)?/
  int, dec = $1.reverse, $2 ? $2 : ""
  while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2'+separator+'\3')
  end
  int.reverse + dec
end