Class: Numeric

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

Overview

Enumerable

Instance Method Summary collapse

Instance Method Details

#commasObject

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



114
115
116
117
118
119
120
# File 'lib/test-factory/core_ext.rb', line 114

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