Skip to content
This repository
Browse code

Added :sign_before_symbol option for displaying negative numbers as "…

…-£1"

rather than "£-1"
  • Loading branch information...
commit 67cb86ceadb2ba0f46a76760947eab70af0be891 1 parent a94debb
Ed Saunders authored August 31, 2012 dougdroper committed January 18, 2013
10  lib/money/money/formatting.rb
@@ -187,12 +187,18 @@ def format(*rules)
187 187
           :after
188 188
         end
189 189
 
  190
+      sign = ""
  191
+      if rules[:sign_before_symbol] == true && self.negative?
  192
+        formatted.tr!("-", "")
  193
+        sign = "-"
  194
+      end
  195
+
190 196
       if symbol_value && !symbol_value.empty?
191 197
         formatted = if symbol_position == :before
192  
-          "#{symbol_value}#{formatted}"
  198
+          "#{sign}#{symbol_value}#{formatted}"
193 199
         else
194 200
           symbol_space = rules[:symbol_after_without_space] ? "" : " "
195  
-          "#{formatted}#{symbol_space}#{symbol_value}"
  201
+          "#{sign}#{formatted}#{symbol_space}#{symbol_value}"
196 202
         end
197 203
       end
198 204
 
11  spec/money/formatting_spec.rb
@@ -343,6 +343,17 @@
343 343
       end
344 344
     end
345 345
 
  346
+    describe ":sign_before_symbol option" do
  347
+      specify "(:sign_before_symbol => true) works as documented" do
  348
+        Money.us_dollar(-100000).format(:sign_before_symbol => true).should == "-$1,000.00"
  349
+      end
  350
+
  351
+      specify "(:sign_before_symbol => false) works as documented" do
  352
+        Money.us_dollar(-100000).format(:sign_before_symbol => false).should == "$-1,000.00"
  353
+        Money.us_dollar(-100000).format(:sign_before_symbol => nil).should == "$-1,000.00"
  354
+      end
  355
+    end
  356
+
346 357
     context "when the monetary value is 0" do
347 358
       let(:money) { Money.us_dollar(0) }
348 359
 

0 notes on commit 67cb86c

Please sign in to comment.
Something went wrong with that request. Please try again.