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
298 299 300 |
# File 'lib/money/money/formatting.rb', line 298 def decimal_mark i18n_format_for(:decimal_mark, :separator, ".") end |
#format(*rules) ⇒ String
Creates a formatted price string according to several rules.
Note that the default rules can be defined through Money.default_formatting_rules hash.
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/money/money/formatting.rb', line 201 def format(*rules) # support for old format parameters rules = normalize_formatting_rules(rules) rules = default_formatting_rules.merge(rules) rules = localize_formatting_rules(rules) rules = translate_formatting_rules(rules) if rules[:translate] thousands_separator = self.thousands_separator decimal_mark = self.decimal_mark escaped_decimal_mark = Regexp.escape(decimal_mark) 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 = symbol_value_from(rules) formatted = self.abs.to_s if rules[:rounded_infinite_precision] formatted.gsub!(/#{decimal_mark}/, '.') unless '.' == decimal_mark formatted = ((BigDecimal(formatted) * currency.subunit_to_unit).round / BigDecimal(currency.subunit_to_unit.to_s)).to_s("F") formatted.gsub!(/\..*/) do |decimal_part| decimal_part << '0' while decimal_part.length < (currency.decimal_places + 1) decimal_part end formatted.gsub!(/\./, decimal_mark) unless '.' == decimal_mark end sign = self.negative? ? '-' : '' if rules[:no_cents] || (rules[:no_cents_if_whole] && cents % currency.subunit_to_unit == 0) formatted = "#{formatted.to_i}" end # Inspiration: https://github.com/rails/rails/blob/16214d1108c31174c94503caced3855b0f6bad95/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb#L72-L79 if rules[:drop_trailing_zeros] formatted = formatted.sub(/(#{escaped_decimal_mark})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_decimal_mark}\z/, '') end has_decimal_value = !!(formatted =~ /#{escaped_decimal_mark}/) 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 = symbol_position_from(rules) if rules[:sign_positive] == true && self.positive? sign = '+' end if rules[:sign_before_symbol] == true sign_before = sign 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_before}#{symbol_value}#{symbol_space}#{sign}#{formatted}" else symbol_space = rules[:symbol_after_without_space] ? "" : " " "#{sign_before}#{sign}#{formatted}#{symbol_space}#{symbol_value}" end else formatted="#{sign_before}#{sign}#{formatted}" end apply_decimal_mark_from_rules(formatted, rules) if has_decimal_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_separator ⇒ Object Also known as: delimiter
294 295 296 |
# File 'lib/money/money/formatting.rb', line 294 def thousands_separator i18n_format_for(:thousands_separator, :delimiter, ",") end |