Integers and Floats
Numeric types in Ruby.
Integers and Floats is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Numeric Types
Ruby has two everyday number types: Integer for whole numbers and Float for decimals.
puts 42.class
puts 3.14.classIntegers Have No Limit
Ruby integers grow as large as your memory allows. No overflow surprises.
big = 2 ** 100
puts bigInteger Division
Dividing two integers gives an integer result, dropping the remainder.
puts 7 / 2
puts 10 / 3Float Division
If either operand is a float, the result is a float with decimals.
puts 7.0 / 2
puts 7 / 2.0Converting 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_iRounding
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.floorFloat 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 populationQuick 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)Frequently asked questions
Is the “Integers and Floats” lesson free?
Yes — the full text of “Integers and Floats” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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
- Integers and Floats
- Arithmetic and Operators
- The Math Module
- Rational and BigDecimal