Class: String

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

Instance Method Summary collapse

Instance Method Details

#to_dObject

call-seq:

str.to_d  -> bigdecimal

Returns the result of interpreting leading characters in str as a BigDecimal.

require 'bigdecimal'
require 'bigdecimal/util'

"0.5".to_d             # => 0.5e0
"123.45e1".to_d        # => 0.12345e4
"45.67 degrees".to_d   # => 0.4567e2

See also BigDecimal::new.



67
68
69
70
71
72
73
# File 'lib/bigdecimal/util.rb', line 67

def to_d
  begin
    BigDecimal(self)
  rescue ArgumentError
    BigDecimal(0)
  end
end