Class: Complex

Inherits:
Numeric
  • Object
show all
Defined in:
lib/bigdecimal/util.rb

Instance Method Summary collapse

Instance Method Details

#to_d(precision = 0) ⇒ Object

call-seq:

cmp.to_d             -> bigdecimal
cmp.to_d(precision)  -> bigdecimal

Returns the value as a BigDecimal. If the imaginary part is not 0, an error is raised

The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.

require 'bigdecimal'
require 'bigdecimal/util'

Complex(0.1234567, 0).to_d(4)   # => 0.1235e0
Complex(Rational(22, 7), 0).to_d(3)   # => 0.314e1
Complex(1, 1).to_d   # raises ArgumentError

See also Kernel.BigDecimal.



164
165
166
167
168
# File 'lib/bigdecimal/util.rb', line 164

def to_d(precision=0)
  BigDecimal(self) unless self.imag.zero? # to raise error

  BigDecimal(self.real, precision)
end