Numeric.to_i / to_f / to_r / to_s
| Since: | Ruby 1.8(2003) |
|---|
Methods for converting numeric types. Covers conversion to integer with to_i, to floating-point with to_f, and to string with to_s, along with base conversion and conversion to rational numbers.
Syntax
number.to_i string.to_i string.to_i(base) # Interprets the string as a base-N number (2–36) and converts to integer # Convert to floating-point. number.to_f string.to_f # Convert to string. number.to_s integer.to_s(base) # Returns a string in the specified base (2–36) # Convert to rational or complex. number.to_r number.to_c
Method List
| Method | Description |
|---|---|
| to_i | Converts to an integer. For strings, only the leading numeric portion is converted; returns 0 if no numeric value is found. |
| to_i(base) | Interprets a string as a number in the specified base and converts it to an integer (base 2–36). |
| to_f | Converts to a floating-point number. Integers become floats with .0 appended. |
| to_s | Converts a number to a string. |
| to_s(base) | Returns an integer as a string in the specified base (2–36). Useful for converting to binary or hexadecimal. |
| to_r | Converts to a rational number (Rational), expressed as a fraction such as 3/1 or 1/3. |
| to_c | Converts to a complex number (Complex). |
| Integer(str) | A Kernel method. Raises an ArgumentError if the value cannot be converted. |
| Float(str) | A Kernel method. Raises an ArgumentError if the value cannot be converted. |
Sample Code
sample_number_to_i_to_f_to_s.rb
puts "42".to_i # 42
puts "3.14".to_i # 3 (decimal part is truncated)
puts "100abc".to_i # 100 (only the leading numeric part is converted)
puts "abc".to_i # 0 (no numeric value found)
puts 3.99.to_i # 3 (decimal part is truncated)
# to_f: convert to floating-point
puts "3.14".to_f # 3.14
puts 42.to_f # 42.0
puts "1e3".to_f # 1000.0 (exponential notation is also converted)
# to_s: convert to string
puts 255.to_s # "255" (decimal)
puts 255.to_s(2) # "11111111" (binary)
puts 255.to_s(8) # "377" (octal)
puts 255.to_s(16) # "ff" (hexadecimal)
# Reverse base conversion: binary string back to integer
puts "11111111".to_i(2) # 255
puts "ff".to_i(16) # 255
# Safe conversion (raises an error on failure)
begin
Integer("abc")
rescue ArgumentError => e
puts e.message # invalid value for Integer(): "abc"
end
# to_r: convert to rational
puts 0.1.to_r # 3602879701896397/36028797018963968 (internal representation)
puts "1/3".to_r # 1/3
puts 3.14.rationalize(0.001) # 135/43 (approximation)
Running the code produces the following output:
ruby number_to_i_to_f_to_s.rb 42 3 100 0 3 3.14 42.0 1000.0 255 11111111 377 ff 255 255 invalid value for Integer(): "abc" 3602879701896397/36028797018963968 1/3 135/43
Notes
Using to_i and to_f is the most common way to convert strings to numbers, but these methods silently return 0 or 0.0 when conversion fails instead of raising an error. When working with user input or other untrusted values, use Integer() or Float() instead to catch invalid values early.
Base conversion with to_s(n) is a distinctive Ruby feature. You can convert to binary (base 2), octal (base 8), or hexadecimal (base 16), and reverse the conversion with to_i(n). This is handy for tasks like converting hex color codes.
Because floating-point numbers are stored in binary internally, 0.1.to_r produces an unusual-looking fraction. When you need precise decimal arithmetic, use a Rational literal (e.g., Rational(1, 3)) or BigDecimal from the start. Combining these with rounding methods allows for more accurate numeric calculations.
If you find any errors or copyright issues, please contact us.