Class: Scruffy::Formatters::Currency
- Defined in:
- lib/scruffy/formatters.rb
Overview
Currency formatter.
Provides formatting for currencies.
Instance Method Summary collapse
-
#format(target, idx, options) ⇒ Object
Formats value marker.
-
#initialize(options = {}) ⇒ Currency
constructor
Returns a new Currency class.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Currency
Returns a new Currency class.
Options:
- precision
-
precision of value
- unit
-
Defaults to ‘$’
- separator
-
Defaults to ‘.’
- delimiter
-
Defaults to ‘,’
- negative_color
-
Color of value marker for negative values. Defaults to ‘red’
- special_negatives
-
If set to true, parenthesizes negative numbers. ie: -$150.50 becomes ($150.50). Defaults to false.
153 154 155 156 157 158 159 160 |
# File 'lib/scruffy/formatters.rb', line 153 def initialize( = {}) @precision = [:precision] || 2 @unit = [:unit] || '$' @separator = [:separator] || '.' @delimiter = [:delimiter] || ',' @negative_color = [:negative_color] || 'red' @special_negatives = [:special_negatives] || false end |
Instance Method Details
#format(target, idx, options) ⇒ Object
Formats value marker.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/scruffy/formatters.rb', line 163 def format(target, idx, ) @separator = "" unless @precision > 0 begin parts = number_with_precision(target, @precision).split('.') if @special_negatives && (target.to_f < 0) number = "(" + @unit + parts[0].to_i.abs.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{@delimiter}") + @separator + parts[1].to_s + ")" else number = @unit + parts[0].to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{@delimiter}") + @separator + parts[1].to_s end if (target.to_f < 0) && @negative_color [:marker_color_override] = @negative_color end number rescue target end end |