9

有时我看到代码使用to_d. ruby 文档甚至声明有一种Float#to_d方法。但是,它不在我的 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_d,或。Float_ 这是怎么回事?StringFixnum

4

1 回答 1

16

如您需要的文档示例中所述

require 'bigdecimal'
require 'bigdecimal/util'
于 2012-09-04T13:04:18.587 回答