Conversion can be done with BigDecimal.new
A Berger <aberger7890@gmail.com> schrieb:
···
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Conversion can be done with BigDecimal.new
A Berger <aberger7890@gmail.com> schrieb:
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
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:
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_fp BigDecimal.new(r)
p BigDecimal.new(f)
# 2x error!
no direct conversion possible.Berg
Hi
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:
I have read that docu (before), but testet only the case without digits:
" ... If omitted or 0, the number of significant digits is determined from
the initial value."
Why does it not work with float? (Ok, I didnt understand the error-msg,
because first tested it with float(no rational involved!)
So its a bug - in most cases I dont want to specify digits.
But if it works with digits, it is manageable (shouldnt be hard to correct
the module: digits=x.to_f.length-1 (for the dot) ? )
Thanks for this hint. I gave up too early.
Berg