On 28/02/2016 7:25 PM, "A Berger" <aberger7890 / gmail.com> wrote:
>
> Hi
> although this doesnt work:
> r=4/3.to_r
> f=r.to_f
>
> p BigDecimal.new(r)
> p BigDecimal.new(f)
> # 2x error!
> no direct conversion possible.
>
> Berg
>

This worked for me:

    r = Rational(4, 3)
    # or in ruby 2.1+:
    #r = 4/3r

    f = r.to_f

    require 'bigdecimal'
    BigDecimal.new r, 20
    #=> #<BigDecimal:2223a88,'0.1333333333 3333333333E1',36(45)>

    BigDecimal.new f, 10
    # => #<BigDecimal:22205e0,'0.1333333333E1',18(36)>

Omitting the second parameter resulted in errors like: "can't omit
precision for a Rational." but that was a no-brainer to resolve in
combination with:
http://ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html#method-c-new
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>