def format(*rules)
rules = normalize_formatting_rules(rules)
if cents == 0
if rules[:display_free].respond_to?(:to_str)
return rules[:display_free]
elsif rules[:display_free]
return "free"
end
end
symbol_value =
if rules.has_key?(:symbol)
if rules[:symbol] === true
symbol
elsif rules[:symbol]
rules[:symbol]
else
""
end
elsif rules[:html]
currency.html_entity
else
symbol
end
formatted = case rules[:no_cents]
when true
"#{self.to_s.to_i}"
else
"#{self.to_s}"
end
if rules[:no_cents_if_whole] && cents % currency.subunit_to_unit == 0
formatted = "#{self.to_s.to_i}"
end
symbol_position =
if rules.has_key?(:symbol_position)
rules[:symbol_position]
elsif currency.symbol_first?
:before
else
:after
end
if symbol_value && !symbol_value.empty?
formatted = (symbol_position == :before ? "#{symbol_value}#{formatted}" : "#{formatted} #{symbol_value}")
end
if rules.has_key?(:decimal_mark) and rules[:decimal_mark] and
rules[:decimal_mark] != decimal_mark
formatted.sub!(decimal_mark, rules[:decimal_mark])
end
thousands_separator_value = thousands_separator
if rules.has_key?(:thousands_separator)
if rules[:thousands_separator] === false or rules[:thousands_separator].nil?
thousands_separator_value = ""
elsif rules[:thousands_separator]
thousands_separator_value = rules[:thousands_separator]
end
end
formatted.gsub!(/(\d)(?=(?:\d{3})+(?:[^\d]|$))/, "\\1#{thousands_separator_value}")
if rules[:with_currency]
formatted << " "
formatted << '<span class="currency">' if rules[:html]
formatted << currency.to_s
formatted << '</span>' if rules[:html]
end
formatted
end