Numbers

View saved

Do arithmetic

Integers and floats are objects with methods.

puts 10 + 3
puts 10 / 3      # integer division => 3
puts 10 / 3.0    # => 3.333...
puts 2**3

Useful methods

Convert and inspect numeric values.

puts 3.14.round
puts 42.to_s
puts "15".to_i

Compare numbers

Comparisons return boolean values.

puts 5 > 3
puts 5 == 5
puts 5.eql?(5.0) # false: different types

Number tips

  • Integer division truncates toward zero for integers
  • Use floats or Rational when fractions matter
  • Big numbers grow automatically
  • Prefer clear parentheses when mixing operators

Comments

One comment per signed-in account. Comments are saved with this page’s URL.