123456789_123456789_123456789_123456789_123456789_

Class: Complex

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Numeric
Instance Chain:
self, ::Numeric
Inherits: Numeric
Defined in: lib/bigdecimal/util.rb

Instance Method Summary

Instance Method Details

#to_dbigdecimal #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.

[ GitHub ]

  
# 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