Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added :sign_before_symbol option for displaying negative numbers as "…

…-£1"

rather than "£-1"
  • Loading branch information...
commit 67cb86ceadb2ba0f46a76760947eab70af0be891 1 parent a94debb
@seddy seddy authored dougdroper committed
Showing with 19 additions and 2 deletions.
  1. +8 −2 lib/money/money/formatting.rb
  2. +11 −0 spec/money/formatting_spec.rb
View
10 lib/money/money/formatting.rb
@@ -187,12 +187,18 @@ def format(*rules)
:after
end
+ sign = ""
+ if rules[:sign_before_symbol] == true && self.negative?
+ formatted.tr!("-", "")
+ sign = "-"
+ end
+
if symbol_value && !symbol_value.empty?
formatted = if symbol_position == :before
- "#{symbol_value}#{formatted}"
+ "#{sign}#{symbol_value}#{formatted}"
else
symbol_space = rules[:symbol_after_without_space] ? "" : " "
- "#{formatted}#{symbol_space}#{symbol_value}"
+ "#{sign}#{formatted}#{symbol_space}#{symbol_value}"
end
end
View
11 spec/money/formatting_spec.rb
@@ -343,6 +343,17 @@
end
end
+ describe ":sign_before_symbol option" do
+ specify "(:sign_before_symbol => true) works as documented" do
+ Money.us_dollar(-100000).format(:sign_before_symbol => true).should == "-$1,000.00"
+ end
+
+ specify "(:sign_before_symbol => false) works as documented" do
+ Money.us_dollar(-100000).format(:sign_before_symbol => false).should == "$-1,000.00"
+ Money.us_dollar(-100000).format(:sign_before_symbol => nil).should == "$-1,000.00"
+ end
+ end
+
context "when the monetary value is 0" do
let(:money) { Money.us_dollar(0) }
Please sign in to comment.
Something went wrong with that request. Please try again.