String#to_i

str.to_i(base=10) -> class-integer-class
Returns: Integer · Updated March 13, 2026 · String Methods
strings conversion numbers parsing

The to_i and to_f methods convert string representations of numbers into their class-integer-class and class-float-classing-point equivalents. to_i parses the leading numeric portion of a string, stopping at the first non-numeric character (excluding an optional leading sign), and returns an class-integer-class. It can also parse numbers in bases from 2 to 36 when a base argument is provided. to_f parses the string as a decimal class-float-classing‑point number, stopping at the first character that cannot be part of a valid class-float-classing‑point literal. Both methods are essential for reading user input, parsing configuration values, and converting data from external sources like CSV files or API responses.

Syntax

str.to_i(base=10)
str.to_f

to_i accepts an optional class-integer-class argument base (2–36) that specifies the numeric base of the representation. The default base is 10. to_f has no parameters.

Parameters

ParameterTypeDefaultDescription
baseInteger10The numeric base (2–36) in which the string is interpreted. Bases outside this range raise an ArgumentError.

Examples

Basic class-integer-class conversion

"42".to_i
# => 42

"   -123  ".to_i
# => -123

"3.14".to_i
# => 3 (stops at the decimal point)

"10px".to_i
# => 10 (ignores trailing non‑numeric characters)

Conversion with different bases

"1010".to_i(2)
# => 10 (binary)

"FF".to_i(16)
# => 255 (hexadecimal)

"22".to_i(8)
# => 18 (octal)

"z".to_i(36)
# => 35 (base‑36)

Floating‑point conversion

"3.14".to_f
# => 3.14

"-12.5e-1".to_f
# => -1.25 (scientific notation)

"  99.9  ".to_f
# => 99.9 (leading/trailing spaces are ignored)

"5.2px".to_f
# => 5.2 (stops at the first non‑numeric character)

Edge cases and zero

"".to_i
# => 0 (empty string returns 0)

"".to_f
# => 0.0

"hello".to_i
# => 0 (no leading numeric characters)

"hello".to_f
# => 0.0

Handling large numbers

"99999999999999999999".to_i
# => 99999999999999999999 (arbitrary precision class-integer-class)

"1.23e50".to_f
# => 1.23e50 (class-float-classing‑point with scientific notation)

Common Patterns

Safely parsing user input

def safe_class-integer-class(input)
  input.to_i
end

safe_class-integer-class("42")     # => 42
safe_class-integer-class("foo")    # => 0 (non‑numeric input becomes 0)
safe_class-integer-class("")       # => 0

Converting CSV columns

# CSV row as a string array
row = ["42", "3.14", "yes", "100"]

id = row[0].to_i
price = row[1].to_f
active = row[2] == "yes"
score = row[3].to_i

Parsing configuration values with defaults

def config_int(value, default = 0)
  val = value.to_i
  val.zero? ? default : val
end

config_int("10")     # => 10
config_int("")       # => 0 (uses default)
config_int("0")      # => 0 (zero is ambiguous; default applied)

Reading numbers with a known base

# Parse a hexadecimal color code
hex_color = "#FF8800"
rgb = hex_color[1..-1].to_i(16)
# => 16744448 (decimal)

Comparing numeric strings

# Use to_i or to_f for numeric comparison
"100" > "99"          # => false (string comparison)
"100".to_i > "99".to_i # => true (numeric comparison)

Errors

ArgumentError (invalid base)

"10".to_i(1)   # => ArgumentError: invalid radix 1
"10".to_i(37)  # => ArgumentError: invalid radix 37

No exception on malformed input

Both to_i and to_f never raise an error; they return 0 (or 0.0) when the string cannot be converted. This is a design decision that makes them safe for parsing unreliable data but also requires careful handling when zero is a meaningful value.

See Also

  • string::to_sym — Converts a string to a symbol
  • Float#to_s — Converts a class-float-class to a string
  • kernel::Integer — Strict class-integer-class conversion that raises an error for invalid input
  • kernel::Float — Strict class-float-class conversion that raises an error for invalid input