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.
Convert Celsius to Fahrenheit, remembering to use Floats in the formula. Run it with ruby temp.rb .
📋 Quick Reference — Numbers & Math
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.