Class: Scruffy::Formatters::Percentage
- Defined in:
- lib/scruffy/formatters.rb
Overview
Percentage formatter.
Provides formatting for percentages.
Instance Method Summary collapse
-
#format(target) ⇒ Object
Formats percentages.
-
#initialize(options = {}) ⇒ Percentage
constructor
Returns new Percentage formatter.
Methods inherited from Base
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( = {}) @precision = [:precision] || 3 @separator = [: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 |