Module: Money::Formatting
- Included in:
- Money
- Defined in:
- lib/money/money/formatting.rb
Instance Method Summary collapse
- #decimal_mark ⇒ Object (also: #separator)
-
#format(*rules) ⇒ String
Creates a formatted price string according to several rules.
- #thousands_separator ⇒ Object (also: #delimiter)
Instance Method Details
#decimal_mark ⇒ Object Also known as: separator
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/money/money/formatting.rb', line 28 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.
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
.
a space between the 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
.
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/money/money/formatting.rb', line 183 def format(*rules) # support for old format parameters rules = normalize_formatting_rules(rules) rules = localize_formatting_rules(rules) if fractional == 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 == '' ? currency.symbol : currency.html_entity else symbol end formatted = rules[:no_cents] ? "#{self.to_s.to_i}" : self.to_s if rules[:no_cents_if_whole] && cents % currency.subunit_to_unit == 0 formatted = "#{self.to_s.to_i}" end thousands_separator_value = thousands_separator # Determine thousands_separator if rules.has_key?(:thousands_separator) thousands_separator_value = rules[:thousands_separator] || '' end # Apply thousands_separator formatted.gsub!(regexp_format(formatted, rules, decimal_mark, symbol_value), "\\1#{thousands_separator_value}") symbol_position = if rules.has_key?(:symbol_position) rules[:symbol_position] elsif currency.symbol_first? :before else :after end sign = "" if rules[:sign_before_symbol] == true && self.negative? formatted.tr!("-", "") sign = "-" end if symbol_value && !symbol_value.empty? symbol_value = "<span class=\"currency_symbol\">#{symbol_value}</span>" if rules[:html_wrap_symbol] formatted = if symbol_position == :before symbol_space = rules[:symbol_before_without_space] === false ? " " : "" "#{sign}#{symbol_value}#{symbol_space}#{formatted}" else symbol_space = rules[:symbol_after_without_space] ? "" : " " "#{sign}#{formatted}#{symbol_space}#{symbol_value}" end end if rules.has_key?(:decimal_mark) && rules[:decimal_mark] && rules[:decimal_mark] != decimal_mark formatted.sub!(decimal_mark, rules[:decimal_mark]) end 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_separator ⇒ Object Also known as: delimiter
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/money/money/formatting.rb', line 6 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 |