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
-
.currency(amount, currency) ⇒ String
Returns a formatted string for the specified currency amount and currency.
-
.level(value) ⇒ String
Returns a formatted string for the specified level.
-
.seconds(value) ⇒ String
Returns a formatted string for the specified number of seconds in the format ‘[<hours>:]<minutes>:<seconds>`.
-
.symbol(value) ⇒ String
Formats the passed symbol into a human-readable string, replacing underscores with spaces and capitalizing the first letter.
Class Method Details
.currency(amount, currency) ⇒ String
Returns a formatted string for the specified currency amount and currency. Two decimal places are used for all currencies except the Japanese Yen.
24 25 26 27 28 29 30 31 32 |
# File 'lib/ig_markets/format.rb', line 24 def currency(amount, currency) return '' unless amount if ['JPY', '¥'].include? currency "#{currency} #{format '%i', amount.to_i}" else "#{currency} #{format '%.2f', amount.to_f}" end end |
.level(value) ⇒ String
Returns a formatted string for the specified level. At most four decimal places are used to format levels.
11 12 13 14 15 |
# File 'lib/ig_markets/format.rb', line 11 def level(value) return '' unless value Float(format('%.4f', value.to_f)).to_s end |
.seconds(value) ⇒ String
Returns a formatted string for the specified number of seconds in the format ‘[<hours>:]<minutes>:<seconds>`.
39 40 41 42 43 44 45 46 47 |
# File 'lib/ig_markets/format.rb', line 39 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 |
.symbol(value) ⇒ String
Formats the passed symbol into a human-readable string, replacing underscores with spaces and capitalizing the first letter.
55 56 57 |
# File 'lib/ig_markets/format.rb', line 55 def symbol(value) value.to_s.capitalize.tr '_', ' ' end |