Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Rational#to_d

rat.to_d(precision) → bigdecimal

Returns the value as a BigDecimal.

The required precision parameter is used to determine the number of significant digits for the result.

Example
require 'bigdecimal'
require 'bigdecimal/util'

Rational(22, 7).to_d(3)   # => 0.314e1

See also BigDecimal::new.

See also #round #to_f #rationalize #to_i also in 3.3 · 3.2 · 3.0