Skip to content

Latest commit

 

History

History
92 lines (78 loc) · 1.03 KB

numbers.md

File metadata and controls

92 lines (78 loc) · 1.03 KB

Numbers

Integers and Floats

  • Ruby numbers has 2 categories: Intergers and Floats.
  • Integers is numbers with no decimal points. Ex: 10, -5
  • Floats is numbers with decimal points. Ex: 1.25, -3.144445

Examples:

# Integer
x = 10
x.class
=> Integer

# Float
x = 5.0
x.class
=> Float
 
x = 10.25
x.class
=> Float

Operaters

  • Addition (+)
3 + 5   #=> 8
3 + 5.5 #=> 8.5
  • Substraction (-)
5 - 3    #=> 2
3 - 5.5  #=> -2.5
  • Multiplication (*)
3 * 5    #=> 15
3 * 5.5  #=> 16.5
  • Division (/)
10/3    #=> 3
10/3.0  #=> 3.3333333333333335
10.0/3  #=> 3.3333333333333335
  • To power (**)
2**3  #=> 8
  • Modulo (%)
10 % 3  #=> 1
10 % 4  #=> 2

Convert numbers

  • Convert to Float: Using to_f
x = 5
x.class
=> Integer
x = x.to_f
=> 5.0
x.class
=> Float
  • Convert to Integer: Using to_i
x = 5.3
x.class
=> Float
x = x.to_i
=> 5
x.class
=> Integer
  • Convert to String: Using to_s
500.to_s
=> "500"
10.22.to_s
=> "10.22"