Class: Float
Instance Method Summary collapse
-
#to_d ⇒ void
Raises a ‘TypeError` exception.
Instance Method Details
#to_d ⇒ void
This method returns an undefined value.
Raises a ‘TypeError` exception. The reason this method is defined at all is to produce a more meaningful error than `NoSuchMethod`.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ruby/to_d.rb', line 69 def to_d # The problem is there isn't a way to know the correct precision, # since there are (many) values that cannot be represented exactly # using Floats. For instance, we can't assume which value is correct # # "%0.10f" % 1.8 #=> "1.8000000000" # "%0.20f" % 1.8 #=> "1.80000000000000004441" # # The programmer should convert the Float to a String using whatever # precision he chooses, and call #to_d on the String. raise TypeError, "cannot convert Float to BigDecimal" end |