Module: Sinatra::Numeric::Helpers
- Defined in:
- lib/sinatra/support/numeric.rb
Instance Method Summary collapse
-
#currency(number, opts = {}) ⇒ String?
Formats a number into a currency display.
-
#percentage(number, precision = 2) ⇒ String?
Show the percentage representation of a numeric value.
Instance Method Details
#currency(number, opts = {}) ⇒ String?
Formats a number into a currency display. Uses the following settings:
-
settings.default_currency_unit (defaults to ‘$’)
-
settings.default_currency_precision (defaults to 2)
-
settings.default_currenty_separator (defaults to ‘,’)
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sinatra/support/numeric.rb', line 75 def currency(number, opts = {}) return if number.to_s.empty? unit = opts[:unit] || settings.default_currency_unit precision = opts[:precision] || settings.default_currency_precision separator = opts[:separator] || settings.default_currency_separator ret = "%s %.#{ Integer(precision) }f" % [unit, number] parts = ret.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{separator}") parts.join('.') end |
#percentage(number, precision = 2) ⇒ String?
Show the percentage representation of a numeric value.
98 99 100 101 102 103 |
# File 'lib/sinatra/support/numeric.rb', line 98 def percentage(number, precision = 2) return if number.to_s.empty? ret = "%02.#{ precision }f%" % number ret.gsub(/\.0*%$/, '%') end |