Float numbers, also known as floating-point numbers, in Ruby are used to represent decimal numbers. They are represented by adding a decimal point to a number or using scientific notation.
Here are some examples of using float numbers in Ruby:
# Declare a float variable
num1 = 3.14
puts num1 # Output: 3.14
# Add two float numbers
num2 = 2.5
sum = num1 + num2
puts sum # Output: 5.64
# Subtract two float numbers
diff = num1 - num2
puts diff # Output: 0.64
# Multiply two float numbers
product = num1 * num2
puts product # Output: 7.85
# Divide two float numbers
quotient = num1 / num2
puts quotient # Output: 1.256
# Use scientific notation
num3 = 1.23e-4
puts num3 # Output: 0.000123
In the above examples, we declare a float variable num1 and assign it the value 3.14. We then perform different arithmetic operations on num1 along with another float variable num2. We also demonstrate using scientific notation to create a float number num3.