Float#to_d在哪里?

时间:2012-09-04 13:01:43

标签: ruby

有时我会看到使用to_d的代码。 ruby文档甚至指出there is a Float#to_d method。但是,它不在我的ruby版本中(ruby 1.9.3p263 (2012-08-23 revision 36792)。

1.9.3p263 :001 > "0.0".to_d
NoMethodError: undefined method `to_d' for "0.0":String
    from (irb):1
    from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
1.9.3p263 :002 > 0.0.to_d
NoMethodError: undefined method `to_d' for 0.0:Float
    from (irb):2
    from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'
1.9.3p263 :003 > 0.to_d
NoMethodError: undefined method `to_d' for 0:Fixnum
    from (irb):3
    from /home/iblue/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'

to_dFloatString中没有Fixnum。发生了什么事?

1 个答案:

答案 0 :(得分:15)

正如您需要的example of the documentation中所述

require 'bigdecimal'
require 'bigdecimal/util'