← Back to Ruby Course | Chapter 2: Variables & Types | Lesson 3 of 7

Float Type

In this page:

  1. Float Type

Float Type

Float represents floating-point (decimal) numbers in Ruby, following standard IEEE 754 double-precision behavior, including the usual floating-point rounding quirks. Dividing two integers with / gives integer division, so at least one operand must be a Float to get a fractional result. to_f converts a value to a float explicitly.

Note: Divide with 10.0 / 3 (or call .to_f on an operand) to force a fractional result instead of integer division.

Example: Float Type

markup
price = 19.99
puts price.class
puts price * 2

puts 10 / 3          # integer division
puts 10.0 / 3         # float division
puts 10.to_f / 3
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.