OneBite.Dev - Coding blog in a bite size

convert variable from float to int in Ruby

Code snippet on how to convert variable from float to int in Ruby

num_float = 15.5
num_int = num_float.to_i

This code snippet shows how to convert a variable from a float to an integer in Ruby. First, the variable “num_float” is set to the float 15.5. Then, a new variable called “num_int” is created and set to the same value as “num_float”, but with the .to_i method, which executes a conversion from float to integer. This means that “num_float” will keep its original value (15.5), but now “num_int” will store the integer 15.

ruby