Class: Money::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/money/money/formatter.rb

Constant Summary collapse

DEFAULTS =
{
  thousands_separator: '',
  decimal_mark: '.'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(money, *rules) ⇒ String

Creates a formatted price string according to several rules.

with the symbol (if present) and ‘%n` will be replaced with the number.

Note that the default rules can be defined through Money.default_formatting_rules hash.

Examples:

Money.us_dollar(0).format(display_free: true)     #=> "free"
Money.us_dollar(0).format(display_free: "gratis") #=> "gratis"
Money.us_dollar(0).format                            #=> "$0.00"
Money.ca_dollar(100).format #=> "$1.00"
Money.ca_dollar(100).format(with_currency: true) #=> "$1.00 CAD"
Money.us_dollar(85).format(with_currency: true)  #=> "$0.85 USD"
Money.us_dollar(100.1).format #=> "$1.001"
Money.us_dollar(100.1).format(rounded_infinite_precision: true) #=> "$1"
Money.us_dollar(100.9).format(rounded_infinite_precision: true) #=> "$1.01"
Money.ca_dollar(100).format(no_cents: true) #=> "$1"
Money.ca_dollar(599).format(no_cents: true) #=> "$5"
Money.ca_dollar(10000).format(no_cents_if_whole: true) #=> "$100"
Money.ca_dollar(10034).format(no_cents_if_whole: true) #=> "$100.34"
Money.new(100, "USD") #=> "$1.00"
Money.new(100, "GBP") #=> "£1.00"
Money.new(100, "EUR") #=> "€1.00"

# Same thing.
Money.new(100, "USD").format(symbol: true) #=> "$1.00"
Money.new(100, "GBP").format(symbol: true) #=> "£1.00"
Money.new(100, "EUR").format(symbol: true) #=> "€1.00"

# You can specify a false expression or an empty string to disable
# prepending a money symbol.§
Money.new(100, "USD").format(symbol: false) #=> "1.00"
Money.new(100, "GBP").format(symbol: nil)   #=> "1.00"
Money.new(100, "EUR").format(symbol: "")    #=> "1.00"

# If the symbol for the given currency isn't known, then it will default
# to "¤" as symbol.
Money.new(100, "AWG").format(symbol: true) #=> "¤1.00"

# You can specify a string as value to enforce using a particular symbol.
Money.new(100, "AWG").format(symbol: "ƒ") #=> "ƒ1.00"

# You can specify a indian currency format
Money.new(10000000, "INR").format(south_asian_number_formatting: true) #=> "1,00,000.00"
Money.new(10000000).format(south_asian_number_formatting: true) #=> "$1,00,000.00"
# Default is to not insert a space.
Money.new(100, "USD").format #=> "$1.00"

# Same thing.
Money.new(100, "USD").format(symbol_before_without_space: true) #=> "$1.00"

# If set to false, will insert a space.
Money.new(100, "USD").format(symbol_before_without_space: false) #=> "$ 1.00"
# Default is to insert a space.
Money.new(100, "USD").format(symbol_position: :after) #=> "1.00 $"

# If set to true, will not insert a space.
Money.new(100, "USD").format(symbol_position: :after, symbol_after_without_space: true) #=> "1.00$"
# If a string is specified, it's value is used.
Money.new(100, "USD").format(decimal_mark: ",") #=> "$1,00"

# If the decimal_mark for a given currency isn't known, then it will default
# to "." as decimal_mark.
Money.new(100, "FOO").format #=> "$1.00"
# If a falsey value is specified, no thousands_separator is used.
Money.new(100000, "USD").format(thousands_separator: false) #=> "1000.00"
Money.new(100000, "USD").format(thousands_separator: nil)   #=> "1000.00"
Money.new(100000, "USD").format(thousands_separator: "")    #=> "1000.00"

# If true is specified, the locale or default thousands_separator is used.
Money.new(100000, "USD").format(thousands_separator: true) #=> "1,000.00"

# If a string is specified, it's value is used.
Money.new(100000, "USD").format(thousands_separator: ".") #=> "$1.000.00"

# If the thousands_separator for a given currency isn't known, then it will
# default to "," as thousands_separator.
Money.new(100000, "FOO").format #=> "$1,000.00"
Money.ca_dollar(570).format(html: true, with_currency: true)
#=> "$5.70 <span class=\"currency\">CAD</span>"
Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
#=> "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
# You can specify to display the sign before the symbol for negative numbers
Money.new(-100, "GBP").format(sign_before_symbol: true)  #=> "-£1.00"
Money.new(-100, "GBP").format(sign_before_symbol: false) #=> "£-1.00"
Money.new(-100, "GBP").format                               #=> "£-1.00"
# You can specify to display the sign with positive numbers
Money.new(100, "GBP").format(sign_positive: true,  sign_before_symbol: true)  #=> "+£1.00"
Money.new(100, "GBP").format(sign_positive: true,  sign_before_symbol: false) #=> "£+1.00"
Money.new(100, "GBP").format(sign_positive: false, sign_before_symbol: true)  #=> "£1.00"
Money.new(100, "GBP").format(sign_positive: false, sign_before_symbol: false) #=> "£1.00"
Money.new(100, "GBP").format                               #=> "£+1.00"
Money.new(10000, "USD").format(disambiguate: false)   #=> "$100.00"
Money.new(10000, "CAD").format(disambiguate: false)   #=> "$100.00"
Money.new(10000, "USD").format(disambiguate: true)    #=> "$100.00"
Money.new(10000, "CAD").format(disambiguate: true)    #=> "C$100.00"
Money.new(10000, "USD").format(disambiguate: false)
#=> "<span class=\"currency_symbol\">$100.00</span>
Money.new(10000, "USD").format(symbol_position: :before) #=> "$100.00"
Money.new(10000, "USD").format(symbol_position: :after)  #=> "100.00 $"
# With the following entry in the translation files:
# en:
#   number:
#     currency:
#       symbol:
#         CAD: "CAD$"
Money.new(10000, "CAD").format(translate: true) #=> "CAD$100.00"
Money.new(89000, :btc).format(drop_trailing_zeros: true) #=> B⃦0.00089
Money.new(110, :usd).format(drop_trailing_zeros: true)   #=> $1.1
Money.new(89000, :btc).format(delimiter_pattern: /(\d)(?=\d)/) #=> B⃦8,9,0.00
Money.new(10000, "USD").format(format: '%u %n') #=> "$ 100.00"
Money.new(10000, "USD").format(format: '<span>%u%n</span>')  #=> "<span>$100.00</span>"

Parameters:

  • rules (Hash)

    The options used to format the string.

Options Hash (*rules):

  • :display_free (Boolean, String) — default: false

    Whether a zero amount of money should be formatted of “free” or as the supplied string.

  • :with_currency (Boolean) — default: false

    Whether the currency name should be appended to the result string.

  • :rounded_infinite_precision (Boolean) — default: false

    Whether the amount of money should be rounded when using Money.infinite_precision

  • :no_cents (Boolean) — default: false

    Whether cents should be omitted.

  • :no_cents_if_whole (Boolean) — default: false

    Whether cents should be omitted if the cent value is zero

  • :symbol (Boolean, String, nil) — default: true

    Whether a money symbol should be prepended to the result string. The default is true. This method attempts to pick a symbol that’s suitable for the given currency.

  • :symbol_before_without_space (Boolean, nil) — default: true

    Whether a space between the money symbol and the amount should be inserted when :symbol_position is :before. The default is true (meaning no space). Ignored if :symbol is false or :symbol_position is not :before.

  • :symbol_after_without_space (Boolean, nil) — default: false

    Whether a space between the amount and the money symbol should be inserted when :symbol_position is :after. The default is false (meaning space). Ignored if :symbol is false or :symbol_position is not :after.

  • :decimal_mark (Boolean, String, nil) — default: true

    Whether the currency should be separated by the specified character or ‘.’

  • :thousands_separator (Boolean, String, nil) — default: true

    Whether the currency should be delimited by the specified character or ‘,’

  • :html (Boolean) — default: false

    Whether the currency should be HTML-formatted. Only useful in combination with :with_currency.

  • :html_wrap (Boolean) — default: false

    Whether all currency parts should be HTML-formatted.

  • :sign_before_symbol (Boolean) — default: false

    Whether the sign should be before the currency symbol.

  • :sign_positive (Boolean) — default: false

    Whether positive numbers should be signed, too.

  • :disambiguate (Boolean) — default: false

    Prevents the result from being ambiguous due to equal symbols for different currencies. Uses the ‘disambiguate_symbol`.

  • :html_wrap_symbol (Boolean) — default: false

    Wraps the currency symbol in a html <span> tag.

  • :symbol_position (Symbol) — default: :before

    ‘:before` if the currency symbol goes before the amount, `:after` if it goes after.

  • :translate (Boolean) — default: true

    ‘true` Checks for custom symbol definitions using I18n.

  • :drop_trailing_zeros (Boolean) — default: false

    Ignore trailing zeros after the decimal mark

  • :delimiter_pattern (Boolean) — default: /(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/

    Regular expression to set the placement for the thousands delimiter

  • :format (String) — default: nil

    Provide a template for formatting. ‘%u` will be replaced

See Also:



233
234
235
236
237
# File 'lib/money/money/formatter.rb', line 233

def initialize(money, *rules)
  @money = money
  @currency = money.currency
  @rules = FormattingRules.new(@currency, *rules)
end

Instance Method Details

#decimal_markObject Also known as: separator



254
255
256
# File 'lib/money/money/formatter.rb', line 254

def decimal_mark
  lookup :decimal_mark
end

#thousands_separatorObject Also known as: delimiter



246
247
248
249
250
251
252
# File 'lib/money/money/formatter.rb', line 246

def thousands_separator
  val = lookup :thousands_separator

  return val unless val == true

  lookup_default :thousands_separator
end

#to_sObject



239
240
241
242
243
244
# File 'lib/money/money/formatter.rb', line 239

def to_s
  return free_text if show_free_text?
  result = format_number
  formatted = append_sign(result)
  append_currency_symbol(formatted)
end