TExcelTAD
Home › Ruby › Ruby Numbers

Ruby Numbers

Ruby has two everyday number types: Integer for whole numbers and Float for decimals. For special needs there are also Rational (exact fractions), Complex and BigDecimal. All of them are objects with useful methods.

Integers

Integers can be as large as memory allows — there is no overflow:

ruby · try it yourself
small = 42
big = 2**100
neg = -17
p small.class
p big
p neg.abs
p 1_000_000 + 1
p 0b1010, 0o17, 0xff
Integer
1267650600228229401496703205376
17
1000001
10
15
255

0b, 0o and 0x write numbers in binary, octal and hexadecimal.

Floats

ruby · try it yourself
p 7.5.class
p 1.5e3
p 10 / 4
p 10 / 4.0
p 10.fdiv(4)
p 0.1 + 0.2
p (0.1 + 0.2).round(2)
Float
1500.0
2
2.5
2.5
0.30000000000000004
0.3

Two things to remember:

  1. Integer division truncates: 10 / 4 is 2. Make one side a float (10 / 4.0) or use fdiv to get 2.5.
  2. Floats are approximate: 0.1 + 0.2 is not exactly 0.3. Round when displaying, and never use floats for money (see Rational/BigDecimal below).

Arithmetic operators

Operator Meaning Example Result
+ - * add, subtract, multiply 6 * 7 42
/ divide 7 / 2 3
% remainder (modulo) 7 % 2 1
** power 2 ** 10 1024
divmod quotient and remainder 7.divmod(2) [3, 1]
ruby · try it yourself
minutes = 135
hours, rest = minutes.divmod(60)
puts "#{hours}h #{rest}min"
p -7 / 2
p -7 % 3
p 2**0.5
2h 15min
-4
2
1.4142135623730951

Note: Ruby's integer division rounds down (toward negative infinity), so -7 / 2 is -4, not -3.

Useful number methods

ruby · try it yourself
p 3.7.round
p 3.14159.round(2)
p 3.7.floor
p 3.2.ceil
p 3.99.to_i
p 12.even?
p 0.zero?
p -5.positive?
p 17.clamp(1, 10)
p 255.to_s(2)
p 10.gcd(4), 10.lcm(4)
p 1234567.digits.sum
4
3.14
3
4
3
true
true
false
10
"11111111"
2
20
28

digits returns the decimal digits from last to first; .sum adds them up.

Repeating with numbers

Integers have iteration methods, which is why Ruby code rarely needs a counting for loop:

ruby · try it yourself
3.times { |i| print i, " " }
puts
1.upto(4) { |i| print i * i, " " }
puts
10.step(1, -3) { |i| print i, " " }
puts
0 1 2
1 4 9 16
10 7 4 1

Exact numbers: Rational and BigDecimal

Rational stores a fraction exactly. Write it with an r suffix:

ruby · try it yourself
third = 1/3r
p third
p third * 3
p 0.1r + 0.2r == 0.3r
p (3/4r).to_f

require "bigdecimal"
price = BigDecimal("19.99")
p (price * 3).to_s("F")
(1/3)
(1/1)
true
0.75
"59.97"

For money, many applications store whole cents as integers (1999) and only format them at the end; BigDecimal is the other common choice.

Math module

ruby · try it yourself
p Math.sqrt(81)
p Math::PI.round(5)
p Math.hypot(3, 4)
p Math.log10(1000)
p Integer.sqrt(50)
9.0
3.14159
5.0
3.0
7

Test yourself

What is the result of 7 / 2 in Ruby?

  1. 3.5
  2. 3
  3. 4
  4. An error

Which expression returns 2.5?

  1. 5 / 2
  2. 5.div(2)
  3. 5.fdiv(2)
  4. 5 % 2

Why is 0.1 + 0.2 == 0.3 false?

  1. Ruby has a bug
  2. Floats are stored in binary and are approximate
  3. == cannot compare floats
  4. 0.3 is an Integer