Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Money#format(:separator) and Money#format(:delimiter)

  • Loading branch information...
commit d53a146a10818c2f82799e15076ac6433cdd2222 1 parent 460ec64
@semmons99 semmons99 authored
Showing with 68 additions and 5 deletions.
  1. +54 −5 lib/money/money.rb
  2. +14 −0 test/money_spec.rb
View
59 lib/money/money.rb
@@ -166,8 +166,8 @@ def zero?
# Creates a formatted price string according to several rules. The following
- # options are supported: :display_free, :with_currency, :no_cents, :symbol
- # and :html.
+ # options are supported: :display_free, :with_currency, :no_cents, :symbol,
+ # :separator, :delimiter and :html.
#
# === +:display_free+
#
@@ -227,6 +227,28 @@ def zero?
#
# Money.new(100, "AWG").format(:symbol => "ƒ") => "ƒ1.00"
#
+ # === +:separator+
+ #
+ # Whether the currency should be separated by the specified character or '.'
+ #
+ # If a string is specified, it's value is used.
+ #
+ # Money.new(100, "USD").format(:separator => ",") => "$1,00"
+ #
+ # === +:delimiter+
+ #
+ # Whether the currency should be delimited by the specified character or ','
+ #
+ # If false is specified, no delimiter is used.
+ #
+ # Money.new(100000, "USD").format(:delimiter => false) => "1000.00"
+ # Money.new(100000, "USD").format(:delimiter => nil) => "1000.00"
+ # Money.new(100000, "USD").format(:delimiter => "") => "1000.00"
+ #
+ # If a string is specified, it's value is used.
+ #
+ # Money.new(100000, "USD").format(:delimiter => ".") => "$1.000.00"
+ #
# === +:html+
#
# Whether the currency should be HTML-formatted. Only useful in combination with +:with_currency+.
@@ -256,15 +278,42 @@ def format(*rules)
else
symbol = SYMBOLS[currency] || "$"
end
-
+
if rules[:no_cents]
formatted = sprintf("#{symbol}%d", cents.to_f / 100)
else
formatted = sprintf("#{symbol}%.2f", cents.to_f / 100)
end
+
+ # Determine separator
+ if rules.has_key?(:separator)
+ if rules[:separator]
+ separator = rules[:separator]
+ else
+ separator = "."
+ end
+ else
+ separator = "."
+ end
+
+ # Apply separator
+ formatted.gsub!(/\./, separator)
- # Commify ("10000" => "10,000")
- formatted.gsub!(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
+ # Determine delimiter
+ if rules.has_key?(:delimiter)
+ if rules[:delimiter] === false or rules[:delimiter].nil?
+ delimiter = ""
+ elsif rules[:delimiter]
+ delimiter = rules[:delimiter]
+ else
+ delimiter = ","
+ end
+ else
+ delimiter = ","
+ end
+
+ # Apply delimiter
+ formatted.gsub!(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/, "\\1#{delimiter}\\2")
if rules[:with_currency]
formatted << " "
View
14 test/money_spec.rb
@@ -285,6 +285,20 @@ def to_money
money = Money.new(100, "XYZ")
money.format.should == "$1.00"
end
+
+ specify "#format(:separator => a separator string) works as documented" do
+ Money.new(100, "BRL").format(:separator => ",").should == "R$1,00"
+ end
+
+ specify "#format(:delimiter => a delimiter string) works as documented" do
+ Money.new(100000, "BRL").format(:delimiter => ".").should == "R$1.000.00"
+ Money.us_dollar(200000).format(:delimiter => "").should == "$2000.00"
+ end
+
+ specify "#format(:delimiter => false) works as documented" do
+ Money.us_dollar(100000).format(:delimiter => false).should == "$1000.00"
+ Money.us_dollar(200000).format(:delimiter => nil).should == "$2000.00"
+ end
specify "#format(:html => true) works as documented" do
string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
Please sign in to comment.
Something went wrong with that request. Please try again.