77
88require 'bigdecimal'
99
10- class Integer < Numeric
10+ class Numeric
1111 # call-seq:
12- # int.to_d -> bigdecimal
12+ # num.to_d -> bigdecimal
13+ # num.to_d(precision) -> bigdecimal
1314 #
14- # Returns the value of +int+ as a BigDecimal.
15- #
16- # require 'bigdecimal'
17- # require 'bigdecimal/util'
18- #
19- # 42.to_d # => 0.42e2
20- #
21- # See also Kernel.BigDecimal.
22- #
23- def to_d
24- BigDecimal ( self )
25- end
26- end
27-
28-
29- class Float < Numeric
30- # call-seq:
31- # float.to_d -> bigdecimal
32- # float.to_d(precision) -> bigdecimal
15+ # Returns the value as a BigDecimal.
3316 #
34- # Returns the value of +float+ as a BigDecimal.
3517 # The +precision+ parameter is used to determine the number of
3618 # significant digits for the result. When +precision+ is set to +0+,
3719 # the number of digits to represent the float being converted is determined
@@ -41,18 +23,16 @@ class Float < Numeric
4123 # require 'bigdecimal'
4224 # require 'bigdecimal/util'
4325 #
44- # 0.5. to_d # => 0.5e0
45- # 1.234 .to_d # => 0.1234e1
46- # 1.234. to_d(2 ) # => 0.12e1
26+ # Rational(22, 7). to_d(3) # => 0.314e1
27+ # 3.14 .to_d(3) # => 0.314e1
28+ # 3. to_d(3 ) # => 0.3e1
4729 #
4830 # See also Kernel.BigDecimal.
49- #
5031 def to_d ( precision = 0 )
5132 BigDecimal ( self , precision )
5233 end
5334end
5435
55-
5636class String
5737 # call-seq:
5838 # str.to_d -> bigdecimal
@@ -108,32 +88,7 @@ def to_digits
10888 # d.to_d # => 0.314e1
10989 #
11090 def to_d
111- self
112- end
113- end
114-
115-
116- class Rational < Numeric
117- # call-seq:
118- # rat.to_d(precision) -> bigdecimal
119- #
120- # Returns the value as a BigDecimal.
121- #
122- # The +precision+ parameter is used to determine the number of
123- # significant digits for the result. When +precision+ is set to +0+,
124- # the number of digits to represent the float being converted is determined
125- # automatically.
126- # The default +precision+ is +0+.
127- #
128- # require 'bigdecimal'
129- # require 'bigdecimal/util'
130- #
131- # Rational(22, 7).to_d(3) # => 0.314e1
132- #
133- # See also Kernel.BigDecimal.
134- #
135- def to_d ( precision = 0 )
136- BigDecimal ( self , precision )
91+ self # override to return the same object
13792 end
13893end
13994
@@ -180,7 +135,7 @@ class NilClass
180135 #
181136 # nil.to_d # => 0.0
182137 #
183- def to_d
184- BigDecimal ( 0 )
138+ def to_d ( precision = 0 )
139+ BigDecimal ( 0 , precision )
185140 end
186141end
0 commit comments