summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/lib/bigdecimal/util.rb
blob: a0235460bd03db453ab777af4fc1ae85540bbd2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#
# BigDecimal utility library.
#
# To use these functions, require 'bigdecimal/util'
#
# The following methods are provided to convert other types to BigDecimals:
#
#   String#to_d -> BigDecimal
#   Float#to_d -> BigDecimal
#   Rational#to_d -> BigDecimal
#
# The following method is provided to convert BigDecimals to other types:
#
#   BigDecimal#to_r -> Rational
#
# ----------------------------------------------------------------------
#
class Float < Numeric
  def to_d
    BigDecimal(self.to_s)
  end
end

class String
  def to_d
    BigDecimal(self)
  end
end

class BigDecimal < Numeric
  # Converts a BigDecimal to a String of the form "nnnnnn.mmm".
  # This method is deprecated; use BigDecimal#to_s("F") instead.
  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
  end
end

class Rational < Numeric
  # Converts a Rational to a BigDecimal
  def to_d(nFig=0)
    num = self.numerator.to_s
    if nFig<=0
      nFig = BigDecimal.double_fig*2+1
    end
    BigDecimal.new(num).div(self.denominator,nFig)
  end
end