7
7
8
8
require 'bigdecimal'
9
9
10
- class Integer < Numeric
10
+ class Numeric
11
11
# call-seq:
12
- # int.to_d -> bigdecimal
12
+ # num.to_d -> bigdecimal
13
+ # num.to_d(precision) -> bigdecimal
13
14
#
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.
33
16
#
34
- # Returns the value of +float+ as a BigDecimal.
35
17
# The +precision+ parameter is used to determine the number of
36
18
# significant digits for the result. When +precision+ is set to +0+,
37
19
# the number of digits to represent the float being converted is determined
@@ -41,18 +23,16 @@ class Float < Numeric
41
23
# require 'bigdecimal'
42
24
# require 'bigdecimal/util'
43
25
#
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
47
29
#
48
30
# See also Kernel.BigDecimal.
49
- #
50
31
def to_d ( precision = 0 )
51
32
BigDecimal ( self , precision )
52
33
end
53
34
end
54
35
55
-
56
36
class String
57
37
# call-seq:
58
38
# str.to_d -> bigdecimal
@@ -108,32 +88,7 @@ def to_digits
108
88
# d.to_d # => 0.314e1
109
89
#
110
90
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
137
92
end
138
93
end
139
94
@@ -180,7 +135,7 @@ class NilClass
180
135
#
181
136
# nil.to_d # => 0.0
182
137
#
183
- def to_d
184
- BigDecimal ( 0 )
138
+ def to_d ( precision = 0 )
139
+ BigDecimal ( 0 , precision )
185
140
end
186
141
end
0 commit comments