Skip to content

Commit

Permalink
[ji] make sure BigDecimal converts toJava with java.lang.Number target
Browse files Browse the repository at this point in the history
... just like the rest of RubyNumerics do handle default conversion
  • Loading branch information
kares committed Apr 9, 2018
1 parent 31ed5e8 commit b0fefa9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Expand Up @@ -1878,7 +1878,7 @@ public IRubyObject zero_p() {

@Override
public <T> T toJava(Class<T> target) {
if (target == BigDecimal.class) {
if (target == BigDecimal.class || target == Number.class) {
return (T) value;
}
return super.toJava(target);
Expand Down
18 changes: 16 additions & 2 deletions test/jruby/test_big_decimal.rb
Expand Up @@ -3,8 +3,22 @@

class TestBigDecimal < Test::Unit::TestCase

def test_bad_to_s_format_strings
assert_equal("0.23", BigDecimal.new("0.23").to_s("F"))
def test_to_s
assert_equal("0.0", BigDecimal.new('0.0').to_s)
assert_equal("0.11111111111e0", BigDecimal.new('0.11111111111').to_s)
assert_equal("0.0", BigDecimal.new('0').to_s)
assert_equal("0.1e-9", BigDecimal.new("1.0e-10").to_s)
end

def test_to_java
# assert_equal java.lang.Long, 1000.to_java.class

assert_equal java.math.BigDecimal, BigDecimal.new('1000.8').to_java.class
assert_equal java.lang.Integer, BigDecimal.new('0.0').to_java(:int).class

number = java.lang.Number
assert_equal java.math.BigDecimal.new('8.0111'), BigDecimal.new('8.0111').to_java(number)
assert_equal java.lang.Long, 1000.to_java(number).class
end

def test_no_singleton_methods_on_bigdecimal
Expand Down

0 comments on commit b0fefa9

Please sign in to comment.