Exercise 3: Convert String to Float
Explore how to convert strings representing numbers into floating-point values in Ruby. This lesson helps you understand the difference between strings and floats, and how to use Ruby methods to safely perform this conversion for effective data manipulation.
We'll cover the following...
We'll cover the following...
Problem statement
Determine which method can be used to turn a string, such as "1.5", into a floating-point number, such as 1.5. Remember, floats and integers are different kinds of numbers. Confirm that you’ve found the right methods by trying them in the widget below.
Note: Assign your solution to the
resultvariable to test the code.
Assume that the input string only contains data that can be safely converted to a floating-point number.
Example
input_string = "1"
result = 1.0
input_string = ""
result = 0.0
input_string = "2.2"
result = 2.2