Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/test-factory/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#commasObject Also known as: add_commas, to_string_with_commas

Converts a number object to a string containing commas every 3rd digit. Adds trailing zeroes if necessary to match round currency amounts.



101
102
103
104
105
106
107
# File 'lib/test-factory/core_ext.rb', line 101

def commas
  self.to_s =~ /([^\.]*)(\..*)?/
  int, dec = $1.reverse, $2 ? ('%.2f' % $2).to_s[/.\d+$/] : ".00"
  while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
  end
  int.reverse + dec
end