Ruby/Number/Numeric class

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Find out the modules Numeric class resides

p Numeric.included_modules # => [Comparable, Kernel]



Get the ceiling or floor of a number (from Float, Integer, or Numeric):

4.65.ceil # => 5
4.65.floor # => 4



Get the next integer with next (or its alias succ):

-24.next # => -23
1.next # => 2
999.next # => 1000



Is a number integer

num = 4
if num.integer?
  puts "Invited guests: " + num.to_s
else
  puts "Only whole persons can come to this party."
end



More Math Methods

# Get the absolute value of a number (Bignum, Complex, Fixnum, Float, Numeric, Rational)
-40.abs # => 40
40.abs # => 40



Or round a number up or down (Float, Integer, or Numeric):

100.45.round # => 100
100.49.round # => 100
100.5.round # => 101
100.6.round # => 101



The integer? method comes from the Numeric class

12.integer? # => true
12.0.integer? # => false
-1.integer? # => true
-12.integer? # => true