Convert flt
to a BigDecimal
and return it.
require 'bigdecimal' require 'bigdecimal/util' 0.5.to_d # => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
Returns a string containing a representation of self. As well as a fixed or exponential form of the float
, the call may return NaN
, Infinity
, and -Infinity
.
Since float
is already a float, returns self
.
Returns the value as a rational.
NOTE: 0.3.to_r isn’t the same as ‘0.3’.to_r. The latter is equivalent to ‘3/10’.to_r, but the former isn’t so.
2.0.to_r #=> (2/1) 2.5.to_r #=> (5/2) -0.75.to_r #=> (-3/4) 0.0.to_r #=> (0/1)
See rationalize.
Returns a string representing obj. The default to_s
prints the object’s class and an encoding of the object id. As a special case, the top-level object that is the initial execution context of Ruby programs returns “main”.
Returns exception’s message (or the name of the exception if no message is set).
Returns a string representing this module or class. For basic classes and modules, this is the name. For singletons, we show information on the thing we’re attached to as well.
Converts the value to a string.
The default format looks like 0.xxxxEnn.
The optional parameter s consists of either an integer; or an optional ‘+’ or ‘ ’, followed by an optional number, followed by an optional ‘E’ or ‘F’.
If there is a ‘+’ at the start of s, positive values are returned with a leading ‘+’.
A space at the start of s returns positive values with a leading space.
If s contains a number, a space is inserted after each group of that many fractional digits.
If s ends with an ‘E’, engineering notation (0.xxxxEnn) is used.
If s ends with an ‘F’, conventional floating point notation is used.
Examples:
BigDecimal.new('-123.45678901234567890').to_s('5F') #=> '-123.45678 90123 45678 9' BigDecimal.new('123.45678901234567890').to_s('+8F') #=> '+123.45678901 23456789' BigDecimal.new('123.45678901234567890').to_s(' F') #=> ' 123.4567890123456789'
Returns the value as an integer (Fixnum
or Bignum
).
If the BigNumber is infinity or NaN, raises FloatDomainError
.
Converts a BigDecimal
to a Rational
.
Returns a new Float
object having approximately the same value as the BigDecimal
number. Normal accuracy limits and built-in errors of binary Float
arithmetic apply.
Returns self.
Convert int
to a BigDecimal
and return it.
require 'bigdecimal' require 'bigdecimal/util' 42.to_d # => #<BigDecimal:1008ef070,'0.42E2',9(36)>
As int
is already an Integer
, all these methods simply return the receiver.
Returns the value as a rational.
1.to_r #=> (1/1) (1<<64).to_r #=> (18446744073709551616/1)
Converts a Rational
to a BigDecimal
.
The required precision
parameter is used to determine the amount of significant digits for the result. See BigDecimal#div
for more information, as it is used along with the denominator
and the precision
for parameters.
r = (22/7.0).to_r # => (7077085128725065/2251799813685248) r.to_d(3) # => #<BigDecimal:1a44d08,'0.314E1',18(36)>
Returns the truncated value as an integer.
Equivalent to Rational#truncate
.
Rational(2, 3).to_i #=> 0 Rational(3).to_i #=> 3 Rational(300.6).to_i #=> 300 Rational(98,71).to_i #=> 1 Rational(-30,2).to_i #=> -15
Return the value as a float.
Rational(2).to_f #=> 2.0 Rational(9, 4).to_f #=> 2.25 Rational(-3, 4).to_f #=> -0.75 Rational(20, 3).to_f #=> 6.666666666666667
Returns the value as a string.
Rational(2).to_s #=> "2/1" Rational(-8, 6).to_s #=> "-4/3" Rational('1/2').to_s #=> "1/2"
Returns a string in an ISO 8601 format (This method doesn’t use the expanded representations).
Date.new(2001,2,3).to_s #=> "2001-02-03"
Returns a string in an ISO 8601 format (This method doesn’t use the expanded representations).
DateTime.new(2001,2,3,4,5,6,'-7').to_s #=> "2001-02-03T04:05:06-07:00"
Returns the value of time as an integer number of seconds since the Epoch.
t = Time.now "%10.5f" % t.to_f #=> "1270968656.89607" t.to_i #=> 1270968656