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 ‘.’



174
175
176
177
# File 'lib/scruffy/formatters.rb', line 174

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

Instance Method Details

#format(target) ⇒ Object

Formats percentages.



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/scruffy/formatters.rb', line 180

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