Class: BigDecimal

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

Overview

BigDecimal extends the native Numeric class to provide the #to_digits and #to_d methods.

When you require BigDecimal in your application, this method will be available on BigDecimal objects.

Instance Method Summary collapse

Instance Method Details

#to_dObject

call-seq:

a.to_d -> bigdecimal

Returns self.



96
97
98
# File 'lib/bigdecimal/util.rb', line 96

def to_d
  self
end

#to_digitsObject

call-seq:

a.to_digits -> string

Converts a BigDecimal to a String of the form “nnnnnn.mmm”. This method is deprecated; use BigDecimal#to_s(“F”) instead.

require 'bigdecimal'
require 'bigdecimal/util'

d = BigDecimal.new("3.14")
d.to_digits
# => "3.14"


82
83
84
85
86
87
88
89
90
# File 'lib/bigdecimal/util.rb', line 82

def to_digits
  if self.nan? || self.infinite? || self.zero?
    self.to_s
  else
    i       = self.to_i.to_s
    _,f,_,z = self.frac.split
    i + "." + ("0"*(-z)) + f
  end
end