Method: Numeric#commatize

Defined in:
lib/epitools/core_ext/numbers.rb

#commatize(char = ",") ⇒ Object

Convert this number to a string, adding commas between each group of 3 digits.

(The “char” argument is optional, and specifies what character to use in between

each group of numbers.)


12
13
14
15
16
17
18
19
# File 'lib/epitools/core_ext/numbers.rb', line 12

def commatize(char=",")
  str = self.is_a?(BigDecimal) ? to_s("F") : to_s

  int, frac = str.split(".")
  int = int.gsub /(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/, "\\1#{char}\\2"

  frac ? "#{int}.#{frac}" : int
end