Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #133 from nubs/master

parsing amounts with a trailing minus sign
  • Loading branch information...
commit 1395dbc3ba45d709286a93d3586c7908584cdc37 2 parents 47c5342 + a2b6639
@semmons99 semmons99 authored
Showing with 16 additions and 4 deletions.
  1. +4 −4 lib/money/money/parsing.rb
  2. +12 −0 spec/money/parsing_spec.rb
View
8 lib/money/money/parsing.rb
@@ -230,16 +230,16 @@ def extract_cents(input, currency = Money.default_currency)
num = input.gsub(/[^\d|\.|,|\'|\-]/, '').strip
# set a boolean flag for if the number is negative or not
- negative = num.split(//).first == "-"
+ negative = num =~ /^-|-$/ ? true : false
# if negative, remove the minus sign from the number
# if it's not negative, the hyphen makes the value invalid
if negative
- num = num.gsub(/^-/, '')
- else
- raise ArgumentError, "Invalid currency amount (hyphen)" if num.include?('-')
+ num = num.sub(/^-|-$/, '')
end
+ raise ArgumentError, "Invalid currency amount (hyphen)" if num.include?('-')
+
#if the number ends with punctuation, just throw it out. If it means decimal,
#it won't hurt anything. If it means a literal period or comma, this will
#save it from being mis-interpreted as a decimal.
View
12 spec/money/parsing_spec.rb
@@ -51,6 +51,18 @@
Money.parse('hellothere').should == empty_price
Money.parse('').should == empty_price
end
+
+ it "handles negative inputs" do
+ five_ninety_five = Money.new(595, 'USD')
+
+ Money.parse("$-5.95").should == -five_ninety_five
+ Money.parse("-$5.95").should == -five_ninety_five
+ Money.parse("$5.95-").should == -five_ninety_five
+ end
+
+ it "raises ArgumentError when unable to detect polarity" do
+ lambda { Money.parse('-$5.95-') }.should raise_error ArgumentError
+ end
end
describe ".from_string" do
Please sign in to comment.
Something went wrong with that request. Please try again.