Class: Scruffy::Formatters::Percentage

Inherits:
Base
  • Object
show all
Defined in:
lib/scruffy/formatters.rb

Overview

Percentage formatter.

Provides formatting for percentages.

Instance Method Summary collapse

Methods inherited from Base

#route_format

Constructor Details

#initialize(options = {}) ⇒ Percentage

Returns new Percentage formatter.

Options:

precision

Defaults to 3.

separator

Defaults to ‘.’



192
193
194
195
# File 'lib/scruffy/formatters.rb', line 192

def initialize(options = {})
  @precision    = options[:precision] || 3
  @separator    = options[:separator] || '.'
end

Instance Method Details

#format(target) ⇒ Object

Formats percentages.



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/scruffy/formatters.rb', line 198

def format(target)
  begin
    number = number_with_precision(target, @precision)
    parts = number.split('.')
    if parts.at(1).nil?
      parts[0] + "%"
    else
      parts[0] + @separator + parts[1].to_s + "%"
    end
  rescue
    target
  end
end