Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/bigdecimal/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ class BigDecimal < Numeric
# d.to_digits # => "3.14"
#
def to_digits
if self.nan? || self.infinite? || self.zero?
self.to_s
else
i = self.to_i.to_s
_,f,_,z = self.frac.split
i + "." + ("0"*(-z)) + f
end
to_s("F")
end

# call-seq:
Expand Down
7 changes: 7 additions & 0 deletions test/bigdecimal/test_bigdecimal_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def test_BigDecimal_to_d
assert_same(x, x.to_d)
end

def test_BigDecimal_to_digits
assert_equal("3.14", BigDecimal("3.14").to_digits)
assert_equal("-3.14", BigDecimal("-3.14").to_digits)
assert_equal("-0.5", BigDecimal("-0.5").to_digits)
assert_equal("-0.05", BigDecimal("-0.05").to_digits)
end

def test_Integer_to_d
assert_equal(BigDecimal(1), 1.to_d)
assert_equal(BigDecimal(2<<100), (2<<100).to_d)
Expand Down