2
# BigDecimal utility library.
4
# To use these functions, require 'bigdecimal/util'
6
# The following methods are provided to convert other types to BigDecimals:
8
# String#to_d -> BigDecimal
9
# Float#to_d -> BigDecimal
10
# Rational#to_d -> BigDecimal
12
# The following method is provided to convert BigDecimals to other types:
14
# BigDecimal#to_r -> Rational
16
# ----------------------------------------------------------------------
30
class BigDecimal < Numeric
31
# Converts a BigDecimal to a String of the form "nnnnnn.mmm".
32
# This method is deprecated; use BigDecimal#to_s("F") instead.
34
if self.nan? || self.infinite? || self.zero?
38
s,f,y,z = self.frac.split
39
i + "." + ("0"*(-z)) + f
43
# Converts a BigDecimal to a Rational.
45
sign,digits,base,power = self.split
46
numerator = sign*digits.to_i
47
denomi_power = power - digits.size # base is always 10
49
Rational(numerator,base ** (-denomi_power))
51
Rational(numerator * (base ** denomi_power),1)
56
class Rational < Numeric
57
# Converts a Rational to a BigDecimal
59
num = self.numerator.to_s
61
nFig = BigDecimal.double_fig*2+1
63
BigDecimal.new(num).div(self.denominator,nFig)