Development

Convert String to Integer in Ruby

Captain Salem 2 min read

Convert String to Integer in Ruby

You will often encounter instances where you must convert a string data type to a numerical value. For example, you may need to convert input from the user to a numerical value before performing any numerical operations or storing it in a database.

In this tutorial, we will explore various methods from the Ruby library to convert a string data type to a numerical value such as int, float, or rational and complex numbers.

Method 1 - Convert String to Int Using the to_i Method

Ruby provides a simple method called to_i that allows us to convert a string value to an integer. Passing the target string to the function will return its integer equivalent or 0 if the input string cannot be converted to an integer.

Example code:

irb(main):002:0> var = "100"
=> "100"
irb(main):003:0> var.to_i
=> 100

The example above demonstrates how to use the to_i function in Ruby to convert a string to an integer type.

If the input string cannot be converted to an integer, the function returns 0 as shown:

=> "geekbits"
irb(main):005:0> var.to_i
=> 0

It’s good to keep in mind that if a string contains alpha-numeric characters, the function will interpret the leading numerical-like characters to an int and ignore the rest.

Example demonstration.

irb(main):006:0> var = '123hello'
=> "123hello"
irb(main):007:0> var.to_i
=> 123

As you can see from the output above, the function takes the leading numeric characters and ignores the non-numeric values.

NOTE: This feature is only applied if the input string has numeric values as the leading characters. If the numeric values come after alphabetical characters, the function returns 0 as shown:

irb(main):012:0> var = "hello123"
=> "hello123"
irb(main):013:0> var.to_i
=> 0

Ruby Convert String to Float

We can also convert a string to a floating point value using the to_f function. The function behaves similarly to the to_i function, except it returns the value as a float.

Example:

irb(main):008:0> var = "3.14159"
=> "3.14159"
irb(main):009:0> var.to_f
=> 3.1415

Running the to_f function with a non-numeric string.

irb(main):010:0> var = "geekbits"
=> "geekbits"
irb(main):011:0> var.to_f
=> 0.0

In this case, the function will return a float value of 0.0

Ruby Convert String to Big Decimal

We can also convert an input string to BigDecimal type using the to_d function. Example:

irb(main):015:0> require 'bigdecimal'
=> true
irb(main):016:0> require 'bigdecimal/util'
=> true
irb(main):017:0> var = "100.232"
=> "100.232"
irb(main):018:0> var.to_d
=> 0.100232e3

The to_d function requires you to import the bigdecimal and big decimal/util modules.

Ruby Convert String to Rational

To convert a string to rational number, use the to_r function as shown:

irb(main):019:0> var = "-3.14159"
=> "-3.14159"
irb(main):020:0> var.to_r
=> (-314159/100000)

Other examples:

irb(main):021:0> '100/5'.to_r
=> (20/1)

irb(main):022:0> '1_234_567'.to_r
=> (1234567/1)

irb(main):023:0> '01 Dec 22'.to_r
=> (1/1)

irb(main):025:0> '01/12/22'.to_r
=> (1/12)

Leading whitespace and extraneous characters past the end of a valid number are ignored. An underscore can separate digit sequences. If there is no valid number at the start of the string, the function returns 0.

Ending…

In this tutorial, you learned how to use various Ruby methods to convert an input type to numerical data, such as integers, floating-point values, decimals, rational numbers, and complex numbers.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.