Returns nil represented as a BigDecimal
.
require 'bigdecimal' require 'bigdecimal/util' nil.to_d # => 0.0
Returns self
as a Complex
object.
Returns self
interpreted as a Complex
object; leading whitespace and trailing garbage are ignored:
'9'.to_c # => (9+0i) '2.5'.to_c # => (2.5+0i) '2.5/1'.to_c # => ((5/2)+0i) '-3/2'.to_c # => ((-3/2)+0i) '-i'.to_c # => (0-1i) '45i'.to_c # => (0+45i) '3-4i'.to_c # => (3-4i) '-4e2-4e-2i'.to_c # => (-400.0-0.04i) '-0.0-0.0i'.to_c # => (-0.0-0.0i) '1/2+3/4i'.to_c # => ((1/2)+(3/4)*i) '1.0@0'.to_c # => (1+0.0i) "1.0@#{Math::PI/2}".to_c # => (0.0+1i) "1.0@#{Math::PI}".to_c # => (-1+0.0i)
Returns Complex zero if the string cannot be converted:
'ruby'.to_c # => (0+0i)
See Kernel#Complex
.
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 Kernel.BigDecimal
.
Returns the result of interpreting leading characters in str
as a rational. Leading whitespace and extraneous characters past the end of a valid number are ignored. Digit sequences can be separated by an underscore. If there is not a valid number at the start of str
, zero is returned. This method never raises an exception.
' 2 '.to_r #=> (2/1) '300/2'.to_r #=> (150/1) '-9.2'.to_r #=> (-46/5) '-9.2e2'.to_r #=> (-920/1) '1_234_567'.to_r #=> (1234567/1) '21 June 09'.to_r #=> (21/1) '21/06/09'.to_r #=> (7/2) 'BWV 1079'.to_r #=> (0/1)
NOTE: “0.3”.to_r isn’t the same as 0.3.to_r. The former is equivalent to “3/10”.to_r, but the latter isn’t so.
"0.3".to_r == 3/10r #=> true 0.3.to_r == 3/10r #=> false
See also Kernel#Rational
.
Returns the result of interpreting leading characters in self
as an integer in the given base
(which must be in (0, 2..36)):
'123456'.to_i # => 123456 '123def'.to_i(16) # => 1195503
With base
zero, string object
may contain leading characters to specify the actual base:
'123def'.to_i(0) # => 123 '0123def'.to_i(0) # => 83 '0b123def'.to_i(0) # => 1 '0o123def'.to_i(0) # => 83 '0d123def'.to_i(0) # => 123 '0x123def'.to_i(0) # => 1195503
Characters past a leading valid number (in the given base
) are ignored:
'12.345'.to_i # => 12 '12345'.to_i(2) # => 1
Returns zero if there is no leading valid number:
'abcdef'.to_i # => 0 '2'.to_i(2) # => 0
Returns the result of interpreting leading characters in self
as a Float:
'3.14159'.to_f # => 3.14159 '1.234e-2'.to_f # => 0.01234
Characters past a leading valid number (in the given base
) are ignored:
'3.14 (pi to two places)'.to_f # => 3.14
Returns zero if there is no leading valid number:
'abcdef'.to_f # => 0.0
Returns self
if self
is a String, or self
converted to a String if self
is a subclass of String.
Returns the value of float
as a BigDecimal
. 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' 0.5.to_d # => 0.5e0 1.234.to_d # => 0.1234e1 1.234.to_d(2) # => 0.12e1
See also Kernel.BigDecimal
.
Returns a string containing a representation of self
; depending of the value of self
, the string representation may contain:
A fixed-point number.
A number in “scientific notation” (containing an exponent).
‘Infinity’.
‘-Infinity’.
‘NaN’ (indicating not-a-number).
3.14.to_s # => “3.14” (10.1**50).to_s # => “1.644631821843879e+50” (10.1**500).to_s # => “Infinity” (-10.1**500).to_s # => “-Infinity” (0.0/0.0).to_s # => “NaN”
Returns self
truncated to an Integer
.
1.2.to_i # => 1 (-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
Returns self
(which is already a Float).
Returns the value as a rational.
2.0.to_r #=> (2/1) 2.5.to_r #=> (5/2) -0.75.to_r #=> (-3/4) 0.0.to_r #=> (0/1) 0.3.to_r #=> (5404319552844595/18014398509481984)
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.
0.3.to_r == 3/10r #=> false "0.3".to_r == 3/10r #=> true
See also Float#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 digits, starting from ‘.’ and counting outwards.
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('-1234567890123.45678901234567890').to_s('5F') #=> '-123 45678 90123.45678 90123 45678 9' BigDecimal('1234567890123.45678901234567890').to_s('+8F') #=> '+12345 67890123.45678901 23456789' BigDecimal('1234567890123.45678901234567890').to_s(' F') #=> ' 1234567890123.4567890123456789'