Skip to content
Browse files

Fix integer coercion bug

Issue 107
  • Loading branch information...
1 parent f21b149 commit be7fc9f0ae2d36ed4e39724ea032cb9daa3dc64c @greyblake greyblake committed Sep 1, 2012
View
12 lib/virtus/coercion/string.rb
@@ -133,10 +133,14 @@ def self.to_boolean(value)
#
# @api public
def self.to_integer(value)
- # coerce to a Float first to evaluate scientific notation (if any)
- # that may change the integer part, then convert to an integer
- coerced = to_float(value)
- ::Float === coerced ? coerced.to_i : coerced
+ if value =~ /\A#{INTEGER_REGEXP}\z/
+ value.to_i
+ else
+ # coerce to a Float first to evaluate scientific notation (if any)
+ # that may change the integer part, then convert to an integer
+ coerced = to_float(value)
+ ::Float === coerced ? coerced.to_i : coerced
+ end
end
# Coerce value to float
View
6 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
@@ -47,4 +47,10 @@
it { should equal(string) }
end
+
+ context 'when integer string is big' do
+ let(:string) { '334490140000101135' }
+
+ it { should == 334490140000101135 }
+ end
end

0 comments on commit be7fc9f

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