Module: FormatHelpers

Defined in:
lib/helpers/format_helpers.rb

Class Method Summary collapse

Class Method Details

.commarize(value) ⇒ String

Add commas to a dollar amount

Examples:

commarize(3901.5) => "3,901.5"

Parameters:

  • value (String)

    a float dollar value

Returns:

  • (String)

    A string with commas added appropriately



27
28
29
# File 'lib/helpers/format_helpers.rb', line 27

def self.commarize(value)
  value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end

.format_float(price, color: nil) ⇒ String

Formats a float to 2 decimal places and the color specified

Examples:

FormatHelpers.format_float(123.4567, color: :green)

Parameters:

  • price (Float)

    The float to format

  • color (Symbol) (defaults to: nil)

    The color

Returns:

  • (String)

    The colored string rounded to 2 decimal places



10
11
12
13
14
15
16
17
# File 'lib/helpers/format_helpers.rb', line 10

def self.format_float(price, color: nil)
  rounded = '%.2f' % price
  if color
    rounded.send(color)
  else
    rounded
  end
end