Skip to content

Commit

Permalink
Added :sign_before_symbol option for displaying negative numbers as "…
Browse files Browse the repository at this point in the history
…-£1"

rather than "£-1"
  • Loading branch information
seddy authored and Douglas Roper committed Jan 18, 2013
1 parent a94debb commit 67cb86c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/money/money/formatting.rb
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions spec/money/formatting_spec.rb
Expand Up @@ -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) }

Expand Down

0 comments on commit 67cb86c

Please sign in to comment.