module Padrino::Helpers::NumberHelpers
def number_to_currency(number, options = {})
# => 1234567890,50 £
number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u")
# => £1234567890,50
number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "")
number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506
number_to_currency(1234567890.506) # => $1,234,567,890.51
number_to_currency(1234567890.50) # => $1,234,567,890.50
==== Examples
%n The number
%u The currency unit
:format:: Sets the format of the output string (defaults to "%u%n"). The field types are:
:delimiter:: Sets the thousands delimiter (defaults to ",").
:separator:: Sets the separator between the units (defaults to ".").
:unit:: Sets the denomination of the currency (defaults to "$").
:precision:: Sets the level of precision (defaults to 2).
==== Options
in the +options+ hash.
Formats a +number+ into a currency string (e.g., $13.65). You can customize the format
#
def number_to_currency(number, options = {}) options.symbolize_keys! defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} currency = I18n.translate(:'number.currency.format', :locale => options[:locale], :raise => true) rescue {} defaults = defaults.merge(currency) precision = options[:precision] || defaults[:precision] unit = options[:unit] || defaults[:unit] separator = options[:separator] || defaults[:separator] delimiter = options[:delimiter] || defaults[:delimiter] format = options[:format] || defaults[:format] separator = '' if precision == 0 begin format.gsub(/%n/, number_with_precision(number, :precision => precision, :delimiter => delimiter, :separator => separator) ).gsub(/%u/, unit) rescue number end end