Module: IGMarkets::Format

Defined in:
lib/ig_markets/format.rb

Overview

This module contains shared methods for formatting different content types for display.

Class Method Summary collapse

Class Method Details

.currency(amount, symbol) ⇒ String

Returns a formatted string for the specified currency amount and currency symbol. Two decimal places are used for all currencies except the Japanese Yen.

Parameters:

  • amount (Float, Fixnum)

    The currency amount to format.

  • symbol (String)

    The currency symbol.

Returns:

  • (String)

    The formatted currency amount, e.g. ‘“USD -130.40”`, `“AUD 539.10”`, `“JPY 3560”`.



13
14
15
16
17
18
19
# File 'lib/ig_markets/format.rb', line 13

def currency(amount, symbol)
  if ['JPY', '¥'].include? symbol
    "#{symbol} #{format '%i', amount.to_i}"
  else
    "#{symbol} #{format '%.2f', amount.to_f}"
  end
end

.seconds(value) ⇒ String

Returns a formatted string for the specified number of seconds in the format ‘[<hours>:]<minutes>:<seconds>`.

Parameters:

  • value (Fixnum)

    The number of seconds to format.

Returns:

  • (String)


26
27
28
29
30
31
32
33
34
# File 'lib/ig_markets/format.rb', line 26

def seconds(value)
  result = if value >= 60 * 60
             "#{value / 60 / 60}:#{Kernel.format('%02i', (value / 60) % 60)}"
           else
             (value / 60).to_s
           end

  result + ':' + Kernel.format('%02i', value % 60)
end