Module: MidwireCommon::NumberBehavior

Defined in:
lib/midwire_common/number_behavior.rb

Overview

Common number behavior

Instance Method Summary collapse

Instance Method Details

#commifyObject

Format a number with commas and a decimal point rubocop:disable Style/PerlBackrefs



8
9
10
11
12
13
14
15
# File 'lib/midwire_common/number_behavior.rb', line 8

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