Module: Money::Formatting

Included in:
Money
Defined in:
lib/money/money/formatting.rb

Instance Method Summary collapse

Instance Method Details

#decimal_markObject Also known as: separator



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/money/money/formatting.rb', line 27

def decimal_mark
  if self.class.use_i18n
    I18n.t(
      :"number.currency.format.separator",
      :default => I18n.t(
        :"number.format.separator",
        :default => (currency.decimal_mark || ".")
      )
    )
  else
    currency.decimal_mark || "."
  end
end

#format(*rules) ⇒ String

Creates a formatted price string according to several rules.

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.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"
# 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 false 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 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"
s = Money.ca_dollar(570).format(:html => true, :with_currency => true)
s #=>  "$5.70 <span class=\"currency\">CAD</span>"

Parameters:

  • *rules (Hash)

    The options used to format the string.

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/money/money/formatting.rb', line 142

def format(*rules)
  # support for old format parameters
  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
  # Determine 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

  # Apply thousands_separator
  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

#thousands_separatorObject Also known as: delimiter



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/money/money/formatting.rb', line 5

def thousands_separator
  if self.class.use_i18n
    I18n.t(
      :"number.currency.format.delimiter",
      :default => I18n.t(
        :"number.format.delimiter",
        :default => (currency.thousands_separator || ",")
      )
    )
  else
    currency.thousands_separator || ","
  end
end