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 ‘.’
174 175 176 177 |
# File 'lib/scruffy/formatters.rb', line 174 def initialize( = {}) @precision = [:precision] || 3 @separator = [: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 |