Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

BigDecimal#{round,truncate,floor,ceil} with no args returns to_i #1631

Merged
merged 1 commit into from

2 participants

@atambo
Collaborator

Fixes #1578

@enebo enebo merged commit 381a6b6 into jruby:jruby-1_7
@enebo enebo added this to the JRuby 1.7.13 milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
33 core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
@@ -1456,7 +1456,7 @@ public IRubyObject finite_p() {
}
@JRubyMethod(name = "floor", optional = 1)
- public IRubyObject floor(IRubyObject[]args) {
+ public IRubyObject floor(IRubyObject[] args) {
if (isNaN) {
return newNaN(getRuntime());
}
@@ -1469,15 +1469,26 @@ public IRubyObject floor(IRubyObject[]args) {
n = RubyNumeric.fix2int(args[0]);
}
+ RubyBigDecimal floor = null;
if (value.scale() > n) { // rounding neccessary
- return new RubyBigDecimal(getRuntime(),
+ floor = new RubyBigDecimal(getRuntime(),
value.setScale(n, RoundingMode.FLOOR));
} else {
- return this;
+ floor = this;
+ }
+
+ if (getRuntime().is1_8()) {
+ return floor;
+ } else {
+ if (args.length == 0) {
+ return floor.to_int19();
+ } else {
+ return floor;
+ }
}
}
- @JRubyMethod(name = "floor", optional =1, compat = CompatVersion.RUBY1_9)
+ @JRubyMethod(name = "floor", optional = 1, compat = CompatVersion.RUBY1_9)
public IRubyObject floor19(IRubyObject[] args) {
if (isNaN || isInfinity()) {
throw getRuntime().newFloatDomainError("Computation results to '" + to_s(args).asJavaString() + "'");
@@ -1557,6 +1568,7 @@ public IRubyObject round(ThreadContext context, IRubyObject[] args) {
int scale = args.length > 0 ? num2int(args[0]) : 0;
RoundingMode mode = (args.length > 1) ? javaRoundingModeFromRubyRoundingMode(args[1]) : getRoundingMode(context.runtime);
// JRUBY-914: Java 1.4 BigDecimal does not allow a negative scale, so we have to simulate it
+ RubyBigDecimal bigDecimal = null;
if (scale < 0) {
// shift the decimal point just to the right of the digit to be rounded to (divide by 10**(abs(scale)))
// -1 -> 10's digit, -2 -> 100's digit, etc.
@@ -1564,9 +1576,18 @@ public IRubyObject round(ThreadContext context, IRubyObject[] args) {
// ...round to that digit
BigDecimal rounded = normalized.setScale(0, mode);
// ...and shift the result back to the left (multiply by 10**(abs(scale)))
- return new RubyBigDecimal(getRuntime(), rounded.movePointLeft(scale));
+ bigDecimal = new RubyBigDecimal(getRuntime(), rounded.movePointLeft(scale));
+ } else {
+ bigDecimal = new RubyBigDecimal(getRuntime(), value.setScale(scale, mode));
+ }
+ if (context.runtime.is1_8()) {
+ return bigDecimal;
} else {
- return new RubyBigDecimal(getRuntime(), value.setScale(scale, mode));
+ if (args.length == 0) {
+ return bigDecimal.to_int19();
+ } else {
+ return bigDecimal;
+ }
}
}
View
25 spec/regression/GH-1578_bigdecimal_to_i_with_no_args.rb
@@ -0,0 +1,25 @@
+require 'bigdecimal'
+
+describe "BigDecimal#round" do
+ it "returns fixnum if no args are passed" do
+ expect(BigDecimal.new('1').round).to be_a(Fixnum)
+ end
+end
+
+describe "BigDecimal#truncate" do
+ it "returns fixnum if no args are passed" do
+ expect(BigDecimal.new('1').truncate).to be_a(Fixnum)
+ end
+end
+
+describe "BigDecimal#floor" do
+ it "returns fixnum if no args are passed" do
+ expect(BigDecimal.new('1').floor).to be_a(Fixnum)
+ end
+end
+
+describe "BigDecimal#ceil" do
+ it "returns fixnum if no args are passed" do
+ expect(BigDecimal.new('1').ceil).to be_a(Fixnum)
+ end
+end
Something went wrong with that request. Please try again.