diff --git a/lib/ofx.rb b/lib/ofx.rb index 6f7b407..4467fa2 100644 --- a/lib/ofx.rb +++ b/lib/ofx.rb @@ -11,15 +11,18 @@ def monetary_vars(*methods) #:nodoc: module MonetarySupport # Returns pennies for a given string amount, i.e: + # '-.45' => -45 # '-123.45' => -12345 # '123' => 12300 def pennies_for(amount) return nil if amount == "" int, fraction = amount.scan(/\d+/) + int, fraction = 0, int if amount.match(/^-?\./) i = (fraction.to_s.strip =~ /[1-9]/) ? "#{int}#{fraction[0,2]}".to_i : int.to_i * 100 - amount =~ /^\s*-\s*\d+/ ? -i : i + amount =~ /^\s*-\s*.?\d+/ ? -i : i end + def original_method(meth) #:nodoc: meth.to_s.sub('_in_pennies','').to_sym rescue nil end diff --git a/test/test_ofx_parser.rb b/test/test_ofx_parser.rb index 00c6fe6..e5ea12d 100644 --- a/test/test_ofx_parser.rb +++ b/test/test_ofx_parser.rb @@ -1,5 +1,6 @@ require 'minitest/autorun' require 'ofx-parser' +require 'pry' class OfxParserTest < MiniTest::Unit::TestCase @@ -459,6 +460,7 @@ def test_for_pennies '1' => 100, '1.0' => 100, '-1.0' => -100, + '-.11' => -11, '' => nil }