-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.rb
More file actions
45 lines (37 loc) · 1.99 KB
/
Copy pathnumber.rb
File metadata and controls
45 lines (37 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Number
# In Ruby, numbers are a fundamental data type that can represent both integers and floating-point numbers.
# Integers are whole numbers without a decimal point, while floating-point numbers (floats) are numbers that contain a decimal point.
# Example of integers
age = 30
year = 2024
# Example of floating-point numbers
pi = 3.14
# You can perform various arithmetic operations with numbers in Ruby, such as addition, subtraction, multiplication, and division.
sum = age + year # Addition
difference = year - age # Subtraction
product = age * pi # Multiplication
quotient = year / age # Division
puts "Sum: #{sum}" # Output: Sum: 2054
puts "Difference: #{difference}" # Output: Difference: 1994
puts "Product: #{product}" # Output: Product: 94.2
puts "Quotient: #{quotient}" # Output: Quotient: 67.46666666666667
# Ruby also provides hexadecimal, octal, and binary literals for representing numbers in different bases.
# Hexadecimal numbers are prefixed with `0x`, octal numbers with `0o`, and binary numbers with `0b`.
hexadecimal = 0x1A # Hexadecimal (26 in decimal)
octal = 0o32 # Octal (26 in decimal)
binary = 0b11010 # Binary (26 in decimal)
puts "Hexadecimal: #{hexadecimal}" # Output: Hexadecimal: 26
puts "Octal: #{octal}" # Output: Octal: 26
puts "Binary: #{binary}" # Output: Binary: 26
# Ruby also support _ as a separator for better readability of large numbers.
# And support math like 0001, 0.0001, 1e-4, 1e+4, etc.
underscore_number = 1_000_000 # for readability
exponential_number = 1e-4 # 0.0001
largest_number = 1e10 # 10000000000
exponential_float = 1.3e+2 # 130.0
largest_float = 1.3e-2 # 0.013
puts "Underscore Number: #{underscore_number}" # Output: Underscore Number: 1000000
puts "Exponential Number: #{exponential_number}" # Output: Exponential Number: 0.0001
puts "Largest Number: #{largest_number}" # Output: Largest Number: 10000000000
puts "Exponential Float: #{exponential_float}" # Output: Exponential Float: 130.0
puts "Largest Float: #{largest_float}" # Output: Largest Float: 0.013