Ruby Academy · Lesson

Integers and Floats

Numeric types in Ruby.

Lesson 1 of 413 steps

Integers and Floats is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Two Numeric Types

Ruby has two everyday number types: Integer for whole numbers and Float for decimals.

puts 42.class
puts 3.14.class

Integers Have No Limit

Ruby integers grow as large as your memory allows. No overflow surprises.

big = 2 ** 100
puts big

Integer Division

Dividing two integers gives an integer result, dropping the remainder.

puts 7 / 2
puts 10 / 3

Float Division

If either operand is a float, the result is a float with decimals.

puts 7.0 / 2
puts 7 / 2.0

Converting Types

Use to_f to make a float and to_i to make an integer. to_i truncates toward zero.

puts 5.to_f
puts 3.9.to_i
puts (-3.9).to_i

Rounding

round rounds to the nearest integer, or to a number of decimals when given an argument.

puts 3.14159.round
puts 3.14159.round(2)

ceil and floor

ceil rounds up, floor rounds down, regardless of the decimal part.

puts 3.2.ceil
puts 3.8.floor

Float Precision Pitfall

Floats are stored in binary and cannot represent every decimal exactly. This is why 0.1 + 0.2 is not exactly 0.3.

puts 0.1 + 0.2
puts (0.1 + 0.2).round(2)

Checking Number Traits

Ask questions like even?, odd?, zero?, and positive?.

puts 4.even?
puts 7.odd?
puts 0.zero?
puts (-5).negative?

Absolute Value and Sign

abs gives the magnitude. Comparisons and clamp keep values in range.

puts (-9).abs
puts 15.clamp(0, 10)

Readable Big Numbers

Use underscores to make large literals readable. Ruby ignores them.

population = 8_000_000
puts population

Quick Check

Check integers and floats.

Recap

You learned numeric types:

  • Integer (unlimited) vs Float
  • integer vs float division
  • to_i/to_f, round, ceil, floor
  • float precision pitfalls, traits, abs, clamp, underscores

Next: arithmetic and operators.

puts (22.0 / 7).round(3)
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Integers and Floats” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Integers and Floats”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Integers and Floats”?

Numeric types in Ruby. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Integers and Floats” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integers and Floats
  2. Arithmetic and Operators
  3. The Math Module
  4. Rational and BigDecimal
← Back to Ruby Academy