Numbers & Math
Ruby is a dynamic, beginner-friendly programming language, and numbers power nearly every program — from totals and counters to prices and percentages.
Learn Numbers & Math in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll do arithmetic, understand the Integer-vs-Float trap, and use the numeric methods Ruby gives every number.
What You'll Learn in This Lesson
1️⃣ Arithmetic and the Division Trap
Ruby's arithmetic operators look familiar: + - * / , plus ** for exponent and % for the remainder. The one surprise is division: when both sides are Integers, Ruby throws away the fraction. Make one side a Float to keep the decimals.
That 7 / 3 == 2 result trips up nearly every beginner. Remember: a Float anywhere in the division "infects" the result with decimals.
2️⃣ Numeric Methods
Because numbers are objects, they carry useful methods. You'll reach for round , floor , and ceil for rounding, even? / odd? for tests, and abs for absolute value. Note to_i truncates rather than rounds.
Your turn. Split a bill with a tip among friends. Replace the three TODO values — keep total and tip as Floats so cents survive — then run it.
Mini-Challenge: A Temperature Converter
Convert Celsius to Fahrenheit, remembering to use Floats in the formula. Run it with ruby temp.rb .
Common Errors (and the fix)
- Unexpected integer division — 1 / 2 is 0 . Use 1.0 / 2 for 0.5 .
- to_i rounds? No — it truncates — 9.9.to_i is 9 . Use round if you want 10.
- "divided by 0" (ZeroDivisionError) — Integer division by zero raises an error. Check the divisor first, or use Floats where 1.0/0 gives Infinity .
- Adding a string and a number — "5" + 1 errors. Convert with "5".to_i + 1 .
- Float surprises — 0.1 + 0.2 isn't exactly 0.3 . Round for display, or use cents/BigDecimal for money.
Pro Tips
- 💡 Underscores in big numbers: write 1_000_000 for readability — Ruby ignores the underscores.
- 💡 fdiv for safe division: 7.fdiv(3) always returns a Float, no Float conversion needed.
- 💡 Random numbers: rand(6) gives 0–5; rand(1..6) rolls a die.
📋 Quick Reference — Numbers & Math
Concept
Ruby Syntax
Exponent / modulo
2 ** 8 / 7 % 3
Float division
7.0 / 3
Round to n places
x.round(2)
Floor / ceil
x.floor / x.ceil
Even? / abs
n.even? / n.abs
Random
rand(1..6)
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Ruby has Integers (whole) and Floats (decimal)
- ✅ Operators: + - * / ** %
- ✅ Integer ÷ Integer truncates — use a Float to keep decimals
- ✅ Numbers have methods: round , floor , ceil , even?
- ✅ Watch Float precision for money; round or use cents
- ✅ Next lesson: Control Flow — make decisions in code
Practice quiz
What does 7 / 3 evaluate to in Ruby?
- 2.33
- 3
- 2
- 2.0
Answer: 2. Integer divided by Integer truncates the fraction, so 7 / 3 is 2.
What does 7.0 / 3 evaluate to?
- 2.3333333333333335
- 2
- 2.0
- 3
Answer: 2.3333333333333335. Making one side a Float keeps the decimals: 7.0 / 3 is about 2.333.
What does 7 % 3 return?
- 2
- 0
- 2.33
- 1
Answer: 1. % is modulo (remainder). 3 goes into 7 twice with 1 left over.
What does 2 ** 8 evaluate to?
- 16
- 256
- 64
- 10
Answer: 256. ** is exponentiation, so 2 ** 8 is 256.
What does 9.9.to_i return?
- 9
- 10
- 9.9
- 9.0
Answer: 9. to_i truncates toward zero, it does not round, so 9.9.to_i is 9.
What does 3.7.round return?
- 3
- 3.7
- 4
- 3.0
Answer: 4. round goes to the nearest integer, so 3.7.round is 4.
What does 3.2.ceil return?
- 3
- 4
- 3.2
- 3.0
Answer: 4. ceil always rounds up, so 3.2.ceil is 4.
Which method always returns a Float result for division?
- div
- quo
- to_i
- fdiv
Answer: fdiv. 7.fdiv(3) always returns a Float, no manual conversion needed.
What does 17.to_s(2) produce?
- "17"
- "10001"
- "11"
- "0x11"
Answer: "10001". to_s(2) gives the base-2 (binary) string, and 17 in binary is 10001.
Why can 0.1 + 0.2 not equal exactly 0.3 in Ruby?
- Ruby has a bug
- Integers overflow
- Floats use binary floating point with tiny rounding errors
- + is overloaded incorrectly
Answer: Floats use binary floating point with tiny rounding errors. Floats are binary floating point, so some decimals can't be represented exactly.
Continue this course
- Previous: Strings & Symbols
- Next: Control Flow