Ruby calls decimal numbers Floats. To create a Float rather than an Integer, just make sure to include a decimal point:

5.class # => Integer
5.0.class # => Float


+ - * / ** (math)

The math methods work mostly like you’d expect, and similarly to the ones for integers.

The main difference to keep in mind is with /. Division with floats works the way that we’re used to — it returns fractional results, as a Float:

12.0 / 5.0 # => 2.4

Try the following and see what you get:

12 / 5
12.0 / 5
12 / 5.0

What did you discover? If either side is a float, float division will be performed.

This is why Integer’s .to_f method can come in handy while doing math; at some point if you need to do division and need a fractional answer, then convert it to a Float first.

One other thing to keep in mind: you can use ** in conjunction with fractions to calculate roots, since 91/2 is the same as the square root of 9, 81/3 is the same as the cube root of 8, etc.

9 ** 0.5 # => 3.0
8 ** (1/3.0) # => 2.0

Test your skills:


Floats can round themselves. Play around with the .round method:

Test your skills:


The rand method that we met earlier can also be called with no arguments, in which case it returns a Float between 0 and 1. This is very handy for e.g. probabilities. Give it a try:


That’s it for Floats! Next up, we’ll look at how we can use these basic data types together with conditionals to start building smart programs that can make decisions based on varying input.