0
0
Rubyprogramming~5 mins

Integer and Float number types in Ruby

Choose your learning style9 modes available
Introduction

We use integers and floats to work with whole numbers and numbers with decimals in programs.

Counting items like apples or books (use integers).
Measuring weight or height that can have decimals (use floats).
Calculating prices with cents in shopping apps.
Doing math like adding, subtracting, or dividing numbers.
Storing age as whole years or temperature with decimals.
Syntax
Ruby
integer_number = 10
float_number = 3.14

Integers are whole numbers without decimals.

Floats are numbers that have decimal points.

Examples
Here, age is an integer and price is a float.
Ruby
age = 25
price = 19.99
Integers can be positive or negative whole numbers. Floats can also be negative.
Ruby
count = 100
temperature = -5.5
Adding an integer and a float results in a float.
Ruby
sum = 5 + 3.2
puts sum
Sample Program

This program shows how to create integer and float variables, add them, and print the results.

Ruby
integer_number = 7
float_number = 2.5
sum = integer_number + float_number
puts "Integer: #{integer_number}"
puts "Float: #{float_number}"
puts "Sum: #{sum}"
OutputSuccess
Important Notes

In Ruby, integers and floats are different types but you can mix them in math.

Division with integers can produce floats if needed.

Summary

Integers are whole numbers without decimals.

Floats are numbers with decimals.

You can do math with both types in Ruby easily.