class Money::Formatter
def append_currency_symbol(formatted_number)
def append_currency_symbol(formatted_number) if rules[:with_currency] formatted_number << " " if rules[:html] formatted_number << "<span class=\"currency\">#{currency.to_s}</span>" elsif rules[:html_wrap] formatted_number << html_wrap(currency.to_s, "currency") else formatted_number << currency.to_s end end formatted_number end
def append_sign(formatted_number)
def append_sign(formatted_number) sign = money.negative? ? '-' : '' if rules[:sign_positive] == true && money.positive? sign = '+' end if rules[:sign_before_symbol] == true sign_before = sign sign = '' end symbol_value = symbol_value_from(rules) if symbol_value && !symbol_value.empty? if rules[:html_wrap_symbol] symbol_value = "<span class=\"currency_symbol\">#{symbol_value}</span>" elsif rules[:html_wrap] symbol_value = html_wrap(symbol_value, "currency-symbol") end rules[:format] .gsub('%u', [sign_before, symbol_value].join) .gsub('%n', [sign, formatted_number].join) else formatted_number = "#{sign_before}#{sign}#{formatted_number}" end end
def decimal_mark
def decimal_mark lookup :decimal_mark end
def extract_whole_and_decimal_parts
def extract_whole_and_decimal_parts fractional = money.fractional.abs # Round the infinite precision part if needed fractional = fractional.round if rules[:rounded_infinite_precision] # Translate subunits into units fractional_units = BigDecimal(fractional) / currency.subunit_to_unit # Split the result and return whole and decimal parts separately fractional_units.to_s('F').split('.') end
def format_decimal_part(value)
def format_decimal_part(value) return nil if currency.decimal_places == 0 && !Money.default_infinite_precision return nil if rules[:no_cents] return nil if rules[:no_cents_if_whole] && value.to_i == 0 # Pad value, making up for missing zeroes at the end value = value.ljust(currency.decimal_places, '0') # Drop trailing zeros if needed value.gsub!(/0*$/, '') if rules[:drop_trailing_zeros] value.empty? ? nil : value end
def format_number
def format_number whole_part, decimal_part = extract_whole_and_decimal_parts # Format whole and decimal parts separately decimal_part = format_decimal_part(decimal_part) whole_part = format_whole_part(whole_part) # Assemble the final formatted amount if rules[:html_wrap] if decimal_part.nil? html_wrap(whole_part, "whole") else [ html_wrap(whole_part, "whole"), html_wrap(decimal_mark, "decimal-mark"), html_wrap(decimal_part, "decimal") ].join end else [whole_part, decimal_part].compact.join(decimal_mark) end end
def format_whole_part(value)
def format_whole_part(value) # Apply thousands_separator value.gsub(rules[:delimiter_pattern]) do |digit_to_delimit| "#{digit_to_delimit}#{thousands_separator}" end end
def free_text
def free_text rules[:display_free].respond_to?(:to_str) ? rules[:display_free] : 'free' end
def html_wrap(string, class_name)
def html_wrap(string, class_name) "<span class=\"money-#{class_name}\">#{string}</span>" end
def initialize(money, *rules)
- See: Money.default_formatting_rules - Money.default_formatting_rules for more information.
Options Hash:
(**rules)
-
:format
(String
) -- Provide a template for formatting. `%u` will be replaced -
:delimiter_pattern
(Boolean
) -- Regular expression to set the placement -
:drop_trailing_zeros
(Boolean
) -- Ignore trailing zeros after -
:translate
(Boolean
) -- `true` Checks for custom -
:symbol_position
(Symbol
) -- `:before` if the currency -
:html_wrap_symbol
(Boolean
) -- Wraps the currency symbol -
:disambiguate
(Boolean
) -- Prevents the result from being ambiguous -
:sign_positive
(Boolean
) -- Whether positive numbers should be -
:sign_before_symbol
(Boolean
) -- Whether the sign should be -
:html_wrap
(Boolean
) -- Whether all currency parts should be HTML-formatted. -
:html
(Boolean
) -- Whether the currency should be -
:thousands_separator
(Boolean, String, nil
) -- Whether -
:decimal_mark
(Boolean, String, nil
) -- Whether the -
:symbol_after_without_space
(Boolean, nil
) -- Whether -
:symbol_before_without_space
(Boolean, nil
) -- Whether -
:symbol
(Boolean, String, nil
) -- Whether a money symbol -
:no_cents_if_whole
(Boolean
) -- Whether cents should be -
:no_cents
(Boolean
) -- Whether cents should be omitted. -
:rounded_infinite_precision
(Boolean
) -- Whether the -
:with_currency
(Boolean
) -- Whether the currency name -
:display_free
(Boolean, String
) -- Whether a zero
Returns:
-
(String)
-
Parameters:
-
rules
(Hash
) -- The options used to format the string.
def initialize(money, *rules) @money = money @currency = money.currency @rules = FormattingRules.new(@currency, *rules) end
def lookup(key)
def lookup(key) return rules[key] || DEFAULTS[key] if rules.has_key?(key) (Money.locale_backend && Money.locale_backend.lookup(key, currency)) || DEFAULTS[key] end
def show_free_text?
def show_free_text? money.zero? && rules[:display_free] end
def symbol_value_from(rules)
def symbol_value_from(rules) if rules.has_key?(:symbol) if rules[:symbol] === true if rules[:disambiguate] && currency.disambiguate_symbol currency.disambiguate_symbol else money.symbol end elsif rules[:symbol] rules[:symbol] else "" end elsif rules[:html] || rules[:html_wrap] currency.html_entity == '' ? currency.symbol : currency.html_entity elsif rules[:disambiguate] && currency.disambiguate_symbol currency.disambiguate_symbol else money.symbol end end
def thousands_separator
def thousands_separator lookup :thousands_separator end
def to_s
def to_s return free_text if show_free_text? result = format_number formatted = append_sign(result) append_currency_symbol(formatted) end