Skip to content

Commit 5f4bb39

Browse files
committed
fix the creation of BigDecimal from a String
follow what MRI is doing and ignore the precision fixes #2650 Sponsored by Lookout Inc.
1 parent aabe84c commit 5f4bb39

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,9 @@ public static RubyBigDecimal newInstance(IRubyObject recv, IRubyObject[] args) {
526526
MathContext context = MathContext.UNLIMITED;
527527

528528
if (args.length == 2) {
529+
System.err.println( "margs" + args[1]);
529530
int digits = (int)args[1].convertToInteger().getLongValue();
531+
System.err.println( "margs" + digits);
530532
if (digits < 0) {
531533
throw runtime.newArgumentError("argument must be positive");
532534
}
@@ -562,6 +564,8 @@ public static RubyBigDecimal newInstance(IRubyObject recv, IRubyObject[] args) {
562564
return new RubyBigDecimal(runtime, (RubyClass)recv, new BigDecimal(((RubyFixnum)args[0]).getLongValue(), context));
563565
} else if (args[0] instanceof RubyBignum) {
564566
return new RubyBigDecimal(runtime, (RubyClass)recv, new BigDecimal(((RubyBignum)args[0]).getBigIntegerValue(), context));
567+
} else {
568+
context = MathContext.UNLIMITED;
565569
}
566570
// fall through to String coercion below
567571
}
@@ -592,7 +596,10 @@ public static RubyBigDecimal newInstance(IRubyObject recv, IRubyObject[] args) {
592596
strValue = NUMBER_PATTERN.matcher(strValue).replaceFirst("$1");
593597

594598
try {
599+
System.err.println("newInstance " + strValue );
600+
System.err.println("newInstance " + context );
595601
decimal = new BigDecimal(strValue, context);
602+
System.err.println("newInstance " + decimal );
596603
} catch(NumberFormatException e) {
597604
if (isOverflowExceptionMode(runtime)) {
598605
throw runtime.newFloatDomainError("exponent overflow");
@@ -1769,6 +1776,7 @@ public IRubyObject sqrt(IRubyObject arg) {
17691776

17701777
@JRubyMethod(name = "to_f")
17711778
public IRubyObject to_f() {
1779+
System.err.println("to_f " + value );
17721780
if (isNaN()) {
17731781
return RubyFloat.newFloat(getRuntime(), Double.NaN);
17741782
}

test/test_bignum.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'test/unit'
2+
require 'bigdecimal'
23

34
class TestBignum < Test::Unit::TestCase
45
# others tested in FixnumBignumAutoconversion test
@@ -41,6 +42,11 @@ def test_bignum_should_respond_to_array_operator
4142
def test_bignum_aref_with_bignum_arg_no_exception
4243
assert_equal(0, (2**64)[2**32])
4344
end
45+
46+
def test_GH_2650
47+
assert_equal(BigDecimal.new("10.91231", 1).to_f, 10.91231)
48+
assert_equal(BigDecimal.new("10.9", 2).to_f, 10.9)
49+
end
4450
# more to come
4551

46-
end
52+
end

0 commit comments

Comments
 (0)