diff --git a/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java b/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java index d98fa6bb970e..f788d92524b1 100644 --- a/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java +++ b/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java @@ -526,7 +526,9 @@ public static RubyBigDecimal newInstance(IRubyObject recv, IRubyObject[] args) { MathContext context = MathContext.UNLIMITED; if (args.length == 2) { + System.err.println( "margs" + args[1]); int digits = (int)args[1].convertToInteger().getLongValue(); + System.err.println( "margs" + digits); if (digits < 0) { throw runtime.newArgumentError("argument must be positive"); } @@ -562,6 +564,8 @@ public static RubyBigDecimal newInstance(IRubyObject recv, IRubyObject[] args) { return new RubyBigDecimal(runtime, (RubyClass)recv, new BigDecimal(((RubyFixnum)args[0]).getLongValue(), context)); } else if (args[0] instanceof RubyBignum) { return new RubyBigDecimal(runtime, (RubyClass)recv, new BigDecimal(((RubyBignum)args[0]).getBigIntegerValue(), context)); + } else { + context = MathContext.UNLIMITED; } // fall through to String coercion below } @@ -592,7 +596,10 @@ public static RubyBigDecimal newInstance(IRubyObject recv, IRubyObject[] args) { strValue = NUMBER_PATTERN.matcher(strValue).replaceFirst("$1"); try { + System.err.println("newInstance " + strValue ); + System.err.println("newInstance " + context ); decimal = new BigDecimal(strValue, context); + System.err.println("newInstance " + decimal ); } catch(NumberFormatException e) { if (isOverflowExceptionMode(runtime)) { throw runtime.newFloatDomainError("exponent overflow"); @@ -1769,6 +1776,7 @@ public IRubyObject sqrt(IRubyObject arg) { @JRubyMethod(name = "to_f") public IRubyObject to_f() { + System.err.println("to_f " + value ); if (isNaN()) { return RubyFloat.newFloat(getRuntime(), Double.NaN); } diff --git a/test/test_bignum.rb b/test/test_bignum.rb index b52ca7e4361d..19fcc2ca0b06 100644 --- a/test/test_bignum.rb +++ b/test/test_bignum.rb @@ -1,4 +1,5 @@ require 'test/unit' +require 'bigdecimal' class TestBignum < Test::Unit::TestCase # others tested in FixnumBignumAutoconversion test @@ -41,6 +42,11 @@ def test_bignum_should_respond_to_array_operator def test_bignum_aref_with_bignum_arg_no_exception assert_equal(0, (2**64)[2**32]) end + + def test_GH_2650 + assert_equal(BigDecimal.new("10.91231", 1).to_f, 10.91231) + assert_equal(BigDecimal.new("10.9", 2).to_f, 10.9) + end # more to come -end \ No newline at end of file +end