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 2b62af265be..79b167cc8dd 100644 --- a/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java +++ b/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java @@ -1354,14 +1354,12 @@ public IRubyObject precs(ThreadContext context) { @JRubyMethod(name = "round", optional = 2) public IRubyObject round(ThreadContext context, IRubyObject[] args) { - final int scale = args.length > 0 ? num2int(args[0]) : 0; - // Special treatment for BigDecimal::NAN and BigDecimal::INFINITY // // If round is called without any argument, we should raise a // FloatDomainError. Otherwise, we don't have to call round ; // we can simply return the number itself. - if (scale == 0 && isInfinity()) { + if (args.length == 0 && isInfinity()) { StringBuilder message = new StringBuilder("Computation results to "); message.append('\'').append(callMethod(context, "to_s")).append('\''); @@ -1373,6 +1371,7 @@ public IRubyObject round(ThreadContext context, IRubyObject[] args) { } } + final int scale = args.length > 0 ? num2int(args[0]) : 0; RoundingMode mode = (args.length > 1) ? javaRoundingModeFromRubyRoundingMode(context.runtime, args[1]) : getRoundingMode(context.runtime); // JRUBY-914: Java 1.4 BigDecimal does not allow a negative scale, so we have to simulate it final RubyBigDecimal bigDecimal; diff --git a/test/mri/bigdecimal/test_bigdecimal.rb b/test/mri/bigdecimal/test_bigdecimal.rb index e258d409426..e7ead1aa188 100644 --- a/test/mri/bigdecimal/test_bigdecimal.rb +++ b/test/mri/bigdecimal/test_bigdecimal.rb @@ -946,6 +946,13 @@ def test_split assert_equal([-1, "Infinity", 10, 0], BigDecimal.new("-Infinity").split) end + def test_round_infinity + assert_equal "Infinity", BigDecimal("Infinity").round(0).to_s + assert_raise(FloatDomainError) do + BigDecimal("Infinity").round + end + end + def test_exponent x = BigDecimal.new('-123.45678901234567890') assert_equal(3, x.exponent)