Float Type
In this page:
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
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
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: