Introduction
We use integers and floats to work with whole numbers and numbers with decimals in programs.
We use integers and floats to work with whole numbers and numbers with decimals in programs.
integer_number = 10 float_number = 3.14
Integers are whole numbers without decimals.
Floats are numbers that have decimal points.
age is an integer and price is a float.age = 25 price = 19.99
count = 100 temperature = -5.5
sum = 5 + 3.2 puts sum
This program shows how to create integer and float variables, add them, and print the results.
integer_number = 7 float_number = 2.5 sum = integer_number + float_number puts "Integer: #{integer_number}" puts "Float: #{float_number}" puts "Sum: #{sum}"
In Ruby, integers and floats are different types but you can mix them in math.
Division with integers can produce floats if needed.
Integers are whole numbers without decimals.
Floats are numbers with decimals.
You can do math with both types in Ruby easily.
Integer in Ruby?num1 = 10 num2 = 3.0 result = num1 / num2 puts result
num = 7.5 puts num.to_i.to_f
a = 8 b = 5 average = ?
Pass the test and this pattern moves unknown